Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #441

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
/* eslint-disable no-console */
'use strict';

// Write your code here
const readline = require('node:readline');
const { generateRandomNumber } = require('./modules/generateRandomNumber');
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');
const { getBullsAndCows } = require('./modules/getBullsAndCows');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const secretNumber = generateRandomNumber();

function promptUserForGuess() {
return new Promise((resolve) => {
rl.question('Enter your 4-digit number: ', (userInput) => {
resolve(userInput);
});
});
}

async function startGame() {
try {
const userInput = await promptUserForGuess();

if (checkIsValidUserInput(userInput)) {
const result = getBullsAndCows(userInput, secretNumber);

console.log(result);
} else {
console.log(
// eslint-disable-next-line max-len
'Invalid number! Please enter a valid 4-digit number with unique digits.',
);
}
} catch (error) {
console.error('An error occurred:', error.message);
} finally {
rl.close();
}
}

startGame();
25 changes: 11 additions & 14 deletions src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
'use strict';

/**
* Checks that the user input is valid.
* Valid user input is a 4-digit number that does not start with 0
* and does not contain any duplicate digits.
*
* @param {string} userInput - The user input
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
if (
userInput.length !== 4 ||
userInput[0] === '0' ||
!/^\d{4}$/.test(userInput) ||
new Set(userInput).size !== 4
) {
return false;
}

return true;
}

module.exports = {
checkIsValidUserInput,
};
module.exports = { checkIsValidUserInput };
22 changes: 10 additions & 12 deletions src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict';

/**
* Generate a random 4-digit number that does not start with 0
* and does not contain any duplicate digits.
*
* @return {number} A random 4-digit number
*/
/* eslint-disable no-console */
/* eslint-disable padding-line-between-statements */
function generateRandomNumber() {
/* Write your code here */
let randomNumber;

do {
randomNumber = Math.floor(Math.random() * 9000) + 1000;
} while (new Set(randomNumber.toString()).size !== 4);

return randomNumber;
}

module.exports = {
generateRandomNumber,
};
module.exports = { generateRandomNumber };
43 changes: 25 additions & 18 deletions src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
'use strict';
function getBullsAndCows(userInput, targetNumber) {
const userInputStr = userInput.toString();
const targetNumberStr = targetNumber.toString();
const result = { bulls: 0, cows: 0 };
const targetNumberDigits = Array.from(targetNumberStr);

/**
* Calculate the number of bulls and cows for a given user input.
* Bulls are digits that are in the correct position.
* Cows are digits that are in the wrong position.
* Assume that the user input and the number to guess
* are always 4-digit numbers.
*
* @param {number} userInput - The user input
* @param {number} numberToGuess - The number to guess
* @return {object} An object containing the number of bulls and cows.
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
for (let i = 0; i < targetNumberStr.length; i++) {
if (userInputStr[i] === targetNumberStr[i]) {
result.bulls += 1;
targetNumberDigits[i] = null;
}
}

for (let i = 0; i < targetNumberStr.length; i++) {
if (userInputStr[i] !== targetNumberStr[i]) {
const indexInGuess = targetNumberDigits.indexOf(userInputStr[i]);

if (indexInGuess !== -1) {
result.cows += 1;
targetNumberDigits[indexInGuess] = null;
}
}
}

return result;
}

module.exports = {
getBullsAndCows,
};
module.exports = { getBullsAndCows };
Loading