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

Solution #433

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
33 changes: 32 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
'use strict';

// Write your code here
const readline = require('node:readline');
const { stdin, stdout, exit } = require('node:process');

const { generateRandomNumber } = require('./modules/generateRandomNumber');
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');
const { getBullsAndCows } = require('./modules/getBullsAndCows');

const PROMPT_MESSAGE = 'Write 4 digits: \n';
const INVALID_INPUT_MESSAGE = 'The input must be 4 uniq digits: \n';
const TRY_AGAIN_MESSAGE = 'Need to try one more time: \n';
const WIN_MESSAGE = 'You win! The correct answer is';

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

stdout.write(PROMPT_MESSAGE);

input.on('line', (text) => {
if (!checkIsValidUserInput(text)) {
stdout.write(INVALID_INPUT_MESSAGE + PROMPT_MESSAGE);
}
Comment on lines +21 to +23

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent the program from proceeding with invalid input, add a return statement after displaying the invalid input message. This will ensure that the user is prompted to input again without calculating bulls and cows.


const { bulls, cows } = getBullsAndCows(text, answer);

if (bulls === 4) {
stdout.write(`${WIN_MESSAGE} ${text}`);
input.close();
exit();
}

stdout.write(`{bulls: ${bulls}, cows: ${cows}} \n${TRY_AGAIN_MESSAGE}`);
});
14 changes: 13 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
switch (true) {
case userInput[0] === '0':
return false;

case isNaN(+userInput):
return false;

case new Set(userInput.split('')).size !== 4:
return false;

default:
return true;
}
}

module.exports = {
Expand Down
15 changes: 14 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@
*
* @return {number} A random 4-digit number
*/

function generateRandomNumber() {
/* Write your code here */
const digits = new Set();

const firstDigit = Math.floor(1 + Math.random() * 9);

digits.add(firstDigit);

while (digits.size < 4) {
const randomDigit = Math.floor(Math.random() * 10);

digits.add(randomDigit);
}

return Number(Array.from(digits).join(''));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation may not guarantee a 4-digit number due to the unordered nature of sets. Consider using an array to maintain the order of digits and ensure the first digit is always non-zero.

}

module.exports = {
Expand Down
22 changes: 21 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,27 @@
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
let bulls = 0;
let cows = 0;

const userInputStr = String(userInput);
const numberToGuessStr = String(numberToGuess);

for (let i = 0; i < userInputStr.length; i++) {
const guessNumber = userInputStr[i];
const rightNumber = numberToGuessStr[i];

if (guessNumber === rightNumber) {
bulls += 1;
} else if (
numberToGuessStr.includes(guessNumber) &&
guessNumber !== rightNumber
) {
cows += 1;
}
}

return { bulls, cows };
}

module.exports = {
Expand Down