From daccddfd66c6dd384049d1863ad8a803fe6032d6 Mon Sep 17 00:00:00 2001 From: Anton Bilous Date: Sat, 23 Mar 2024 16:12:52 +0200 Subject: [PATCH] solution --- src/app.js | 32 +++++++++++++++++++++++++++- src/modules/checkIsValidUserInput.js | 20 +++++++++++++++++ src/modules/generateRandomNumber.js | 22 +++++++++++++++++++ src/modules/getBullsAndCows.js | 15 +++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index e89a2d97..f34ac010 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,33 @@ '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(); + +function guess() { + terminal.question('Enter 4 different numbers: ', (data) => { + if (checkIsValidUserInput(data)) { + // eslint-disable-next-line no-console + console.log(getBullsAndCows(+data, +numberToGuess)); + } else { + // eslint-disable-next-line no-console + console.log('Invalid input. Please enter 4 different numbers: '); + } + + if (+data === numberToGuess) { + // eslint-disable-next-line no-console + console.log('You are a winner'); + terminal.close(); + + return; + } + + return guess(); + }); +} + +guess(); diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 40979664..426f2ae0 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -10,6 +10,26 @@ */ function checkIsValidUserInput(userInput) { /* Write your code here */ + + if (userInput.length !== 4 || Number(userInput[0]) === 0) { + return false; + } + + const amount = {}; + + for (let i = 0; i < userInput.length; i++) { + if (isNaN(Number(userInput[i]))) { + return false; + } + + if (!amount[userInput[i]]) { + amount[userInput[i]] = 1; + } else { + return false; + } + } + + return true; } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..9901ef83 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -8,6 +8,28 @@ */ function generateRandomNumber() { /* Write your code here */ + const numberToGuess = []; + + for (let i = 0; i < 4; ) { + const min = 0; + const max = 9; + + const randmonNumber = Math.floor(Math.random() * (max - min + 1) + min); + + if (!numberToGuess.includes(randmonNumber)) { + i++; + + numberToGuess.push(randmonNumber); + } + + continue; + } + + if (numberToGuess[0] === 0) { + return generateRandomNumber(); + } + + return Number(numberToGuess.join('')); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..30ebfb44 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -14,6 +14,21 @@ */ function getBullsAndCows(userInput, numberToGuess) { /* Write your code here */ + let bulls = 0; + let cows = 0; + + const input = String(userInput); + const guess = String(numberToGuess); + + for (let i = 0; i < input.length; i++) { + if (input[i] === guess[i]) { + bulls++; + } else if (input.includes(guess[i])) { + cows++; + } + } + + return { bulls, cows }; } module.exports = {