From 9588694b0c153f2211e78081f7cf72158c04f160 Mon Sep 17 00:00:00 2001 From: Katya Date: Thu, 8 Aug 2024 12:49:30 +0300 Subject: [PATCH] solution --- src/app.js | 40 +++++++++++++++++++++++++++- src/modules/checkIsValidUserInput.js | 29 +++++++++++++++++++- src/modules/generateRandomNumber.js | 22 ++++++++++++++- src/modules/getBullsAndCows.js | 19 ++++++++++++- 4 files changed, 106 insertions(+), 4 deletions(-) diff --git a/src/app.js b/src/app.js index e89a2d97..23f01925 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,41 @@ +/* eslint-disable no-console */ '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 rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +const numberToGuess = generateRandomNumber().toString(); + +function askUserForGuess() { + rl.question('Enter your guess: ', (userInput) => { + if (!checkIsValidUserInput(userInput)) { + console.log( + 'Invalid input. Please enter a 4-digit number without duplicates.', + ); + askUserForGuess(); + + return; + } + + const result = getBullsAndCows(userInput, numberToGuess); + + console.log(`Bulls: ${result.bulls}, Cows: ${result.cows}`); + + if (result.bulls === 4) { + console.log('Congratulations! You guessed the number.'); + rl.close(); + } else { + askUserForGuess(); + } + }); +} + +askUserForGuess(); diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 40979664..c14ec42b 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -8,8 +8,35 @@ * @param {string} userInput - The user input * @return {boolean} - True if the user input is valid, false otherwise */ + +function hasRepeatingdigits(numStr) { + for (let i = 0; i < numStr.length; i++) { + if (numStr.substring(i + 1).includes(numStr[i])) { + return true; + } + } + + return false; +} + function checkIsValidUserInput(userInput) { - /* Write your code here */ + // if ( + // userInput.length !== 4 || + // parseFloat(userInput[0]) === 0 || + // hasRepeatingdigits(userInput) + // ) { + // return false; + // } + + if (!/^\d{4}$/.test(userInput)) { + return false; + } + + if (userInput[0] === '0' || hasRepeatingdigits(userInput)) { + return false; + } + + return true; } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..1d05b34e 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -6,8 +6,28 @@ * * @return {number} A random 4-digit number */ +const LENGTH = 4; + +function generateDigit() { + return Math.floor(Math.random() * 10); +} + function generateRandomNumber() { - /* Write your code here */ + let strNum = ''; + + while (strNum.length < LENGTH) { + const digit = generateDigit().toString(); + + if (strNum.length === 0 && digit === '0') { + continue; + } + + if (!strNum.includes(digit)) { + strNum += digit; + } + } + + return parseFloat(strNum); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..376cee80 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -12,8 +12,25 @@ * @return {object} An object containing the number of bulls and cows. * Example: { bulls: 1, cows: 2 } */ + function getBullsAndCows(userInput, numberToGuess) { - /* Write your code here */ + const bullsAndCows = { + bulls: 0, + cows: 0, + }; + + const strToGuess = `${numberToGuess}`; + const strInput = `${userInput}`; + + for (let i = 0; i < strInput.length; i++) { + if (strInput[i] === strToGuess[i]) { + bullsAndCows.bulls += 1; + } else if (strToGuess.includes(strInput[i])) { + bullsAndCows.cows += 1; + } + } + + return bullsAndCows; } module.exports = {