From 020d2b635670bf6dabc08f5aa58a1667836258f2 Mon Sep 17 00:00:00 2001 From: Valentyn Radobenko Date: Tue, 29 Oct 2024 15:54:21 +0200 Subject: [PATCH 1/2] add solution --- src/modules/checkIsValidUserInput.js | 26 +++++++++++++++++++++++++- src/modules/generateRandomNumber.js | 16 +++++++++++++++- src/modules/getBullsAndCows.js | 20 +++++++++++++++++++- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 40979664..2ec09083 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -9,7 +9,31 @@ * @return {boolean} - True if the user input is valid, false otherwise */ function checkIsValidUserInput(userInput) { - /* Write your code here */ + if (isNaN(+userInput)) { + return false; + } + + if (userInput.length !== 4) { + return false; + } + + if (userInput[0] === '0') { + return false; + } + + for (let i = 0; i < userInput.length; i++) { + for (let j = 0; j < userInput.length; j++) { + if (i === j) { + continue; + } + + if (userInput[i] === userInput[j]) { + return false; + } + } + } + + return true; } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..5d1c6b9d 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -7,7 +7,21 @@ * @return {number} A random 4-digit number */ function generateRandomNumber() { - /* Write your code here */ + let resultStr = ''; + + while (resultStr.length < 4) { + const randomDigitStr = String(Math.floor(Math.random() * 10)); + + if (resultStr.length === 0) { + if (randomDigitStr !== '0') { + resultStr += randomDigitStr; + } + } else if (!resultStr.includes(randomDigitStr)) { + resultStr += randomDigitStr; + } + } + + return +resultStr; } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..3c01bafc 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -13,7 +13,25 @@ * Example: { bulls: 1, cows: 2 } */ function getBullsAndCows(userInput, numberToGuess) { - /* Write your code here */ + const numberToGuessStr = String(numberToGuess); + const userInputStr = String(userInput); + const numberToGuessStrArr = numberToGuessStr.split(''); + const userInputStrArr = userInputStr.split(''); + + const result = { + bulls: 0, + cows: 0, + }; + + for (let i = 0; i < userInputStrArr.length; i++) { + if (userInputStrArr[i] === numberToGuessStrArr[i]) { + result.bulls++; + } else if (numberToGuessStr.includes(userInputStrArr[i])) { + result.cows++; + } + } + + return result; } module.exports = { From 439cb980a2cebd3df88bd08871ba0ab1b19028e4 Mon Sep 17 00:00:00 2001 From: Valentyn Radobenko Date: Tue, 29 Oct 2024 17:11:23 +0200 Subject: [PATCH 2/2] add solution --- src/app.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index e89a2d97..928f5a69 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,42 @@ 'use strict'; -// Write your code here +const readline = require('readline'); +const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput'); +const { generateRandomNumber } = require('./modules/generateRandomNumber'); +const { getBullsAndCows } = require('./modules/getBullsAndCows'); + +const terminal = readline.createInterface(process.stdin, process.stdout); + +const numberToGuess = generateRandomNumber(); +let isFirstAttempt = true; +const print = (message) => terminal.write(message + '\n'); + +const askQuestion = () => { + const prompt = isFirstAttempt + ? `I generated a number of 4 different digits. Please, guess a number :)\n` + : 'Your guess:\n'; + + terminal.question(prompt, (userInput) => { + isFirstAttempt = false; + + if (!checkIsValidUserInput(userInput)) { + print('Your input is invalid, please, try again :)'); + + return askQuestion(); + } + + const { bulls, cows } = getBullsAndCows(userInput, numberToGuess); + + print(`bulls: ${bulls}, cows: ${cows}\n`); + + if (bulls === 4) { + print('Congratulations!'); + + return terminal.close(); + } + + askQuestion(); + }); +}; + +askQuestion();