From 5a1995391e9ad579ca94f6cae9656488983f40ea Mon Sep 17 00:00:00 2001 From: ab3MN Date: Mon, 2 Dec 2024 10:09:58 +0200 Subject: [PATCH 1/3] feat:implemented Solution --- src/app.js | 33 +++++++++++++++++++++++++++- src/modules/checkIsValidUserInput.js | 14 +++++++++++- src/modules/generateRandomNumber.js | 11 +++++++++- src/modules/getBullsAndCows.js | 22 ++++++++++++++++++- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/app.js b/src/app.js index e89a2d97..dbeefa4f 100644 --- a/src/app.js +++ b/src/app.js @@ -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 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); + } + + 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}`); +}); diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 40979664..dcef27ff 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -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 = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..262980c3 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -6,8 +6,17 @@ * * @return {number} A random 4-digit number */ + function generateRandomNumber() { - /* Write your code here */ + const uniqRandomNumbers = new Set(); + + while (uniqRandomNumbers.size !== 4) { + const randomNumber = Math.floor(1 + Math.random() * 9); + + uniqRandomNumbers.add(randomNumber); + } + + return Number(Array.from(uniqRandomNumbers).join('')); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..d5394a0f 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -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 = { From 2cb6a51cdae0298aad3dc031698fc4912ad38864 Mon Sep 17 00:00:00 2001 From: ab3MN Date: Mon, 2 Dec 2024 10:13:16 +0200 Subject: [PATCH 2/3] refactor:refactor INVALID_INPUT_MESSAGE --- src/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index dbeefa4f..8a6c50b8 100644 --- a/src/app.js +++ b/src/app.js @@ -8,7 +8,7 @@ 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 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'; From 7a5cdb8bea1b310a32f1474e6f9af70c22d93e2f Mon Sep 17 00:00:00 2001 From: ab3MN Date: Mon, 2 Dec 2024 10:34:38 +0200 Subject: [PATCH 3/3] refactor:fix all the AI comments --- src/app.js | 2 +- src/modules/generateRandomNumber.js | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/app.js b/src/app.js index 8a6c50b8..3d938e91 100644 --- a/src/app.js +++ b/src/app.js @@ -19,7 +19,7 @@ stdout.write(PROMPT_MESSAGE); input.on('line', (text) => { if (!checkIsValidUserInput(text)) { - stdout.write(INVALID_INPUT_MESSAGE); + stdout.write(INVALID_INPUT_MESSAGE + PROMPT_MESSAGE); } const { bulls, cows } = getBullsAndCows(text, answer); diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 262980c3..c60e6ffa 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -8,15 +8,19 @@ */ function generateRandomNumber() { - const uniqRandomNumbers = new Set(); + const digits = new Set(); - while (uniqRandomNumbers.size !== 4) { - const randomNumber = Math.floor(1 + Math.random() * 9); + const firstDigit = Math.floor(1 + Math.random() * 9); - uniqRandomNumbers.add(randomNumber); + digits.add(firstDigit); + + while (digits.size < 4) { + const randomDigit = Math.floor(Math.random() * 10); + + digits.add(randomDigit); } - return Number(Array.from(uniqRandomNumbers).join('')); + return Number(Array.from(digits).join('')); } module.exports = {