From f94d75086f20ae4b5eaf8fd87dff195525a345ac Mon Sep 17 00:00:00 2001 From: Ruslan Ogerenko Date: Wed, 9 Oct 2024 12:43:34 +0300 Subject: [PATCH 1/2] chek the first function --- src/modules/checkIsValidUserInput.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 40979664..74984da2 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -10,6 +10,20 @@ */ function checkIsValidUserInput(userInput) { /* Write your code here */ + // const userInputStr = userInput.toString(); + + const is4digitNumber = userInput.length === 4; + const isAllAreNumbers = userInput.split('').every((x) => /^[0-9]$/.test(x)); + const isNotStartZero = userInput[0] !== '0'; + const isNotDuplicates = userInput + .split('') + .every((x, _, arr) => arr.indexOf(x) === arr.lastIndexOf(x)); + + if (is4digitNumber && isAllAreNumbers && isNotStartZero && isNotDuplicates) { + return true; + } else { + return false; + } } module.exports = { From aaf507f6ff86115b7f42d0a9f90671a2a15e7d86 Mon Sep 17 00:00:00 2001 From: Ruslan Ogerenko Date: Wed, 9 Oct 2024 23:53:08 +0300 Subject: [PATCH 2/2] solution --- src/app.js | 61 +++++++++++++++++++++++++++- src/modules/checkIsValidUserInput.js | 3 -- src/modules/generateRandomNumber.js | 13 ++++++ src/modules/getBullsAndCows.js | 17 +++++++- 4 files changed, 89 insertions(+), 5 deletions(-) diff --git a/src/app.js b/src/app.js index e89a2d97..5882071e 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,62 @@ 'use strict'; +/* eslint-disable no-console */ -// 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 guessNumber = generateRandomNumber(); +let count = 0; + +function playGame(question = '') { + terminal.question(question, (userInput) => { + if (!checkIsValidUserInput(userInput)) { + console.log( + 'It is not valid number! Input the 4 dight number, without duplicates!', + ); + playGame('Try again!'); + } else { + count++; + + const result = getBullsAndCows(userInput, guessNumber); + + console.log(result, '(Try', count, ')'); + + if (result.bulls === 4) { + console.log( + 'Congratulation!!! You win!!! Number is... ', + guessNumber, + ` [From ${count} try!]`, + ); + terminal.close(); + } else { + const { bulls, cows } = result; + + if (bulls === 0 && cows === 0) { + playGame('Dont worry, it happens...'); + } + + if (bulls === 1 || cows === 1) { + playGame('You are on the right track!'); + } + + if ( + bulls === 2 || + bulls === 3 || + cows === 2 || + cows === 3 || + cows === 4 + ) { + playGame('So close... Come on!'); + } + } + } + }); +} + +console.log('Lets play the game "Bulls and Cows"!!!'); +console.log('Input the 4 dight number'); +playGame(); diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 74984da2..3bae1644 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -9,9 +9,6 @@ * @return {boolean} - True if the user input is valid, false otherwise */ function checkIsValidUserInput(userInput) { - /* Write your code here */ - // const userInputStr = userInput.toString(); - const is4digitNumber = userInput.length === 4; const isAllAreNumbers = userInput.split('').every((x) => /^[0-9]$/.test(x)); const isNotStartZero = userInput[0] !== '0'; diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..e45c5496 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -8,6 +8,19 @@ */ function generateRandomNumber() { /* Write your code here */ + const availableNums = [1, 2, 3, 4, 5, 6, 7, 8, 9]; + const result = []; + + const getRandomN = () => Math.floor(Math.random() * availableNums.length); + + result.push(availableNums.splice(getRandomN(), 1)[0]); + availableNums.unshift(0); + + while (result.length < 4) { + result.push(availableNums.splice(getRandomN(), 1)[0]); + } + + return Number(result.join('')); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..8c651b58 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -13,7 +13,22 @@ * Example: { bulls: 1, cows: 2 } */ function getBullsAndCows(userInput, numberToGuess) { - /* Write your code here */ + const userInputArr = userInput.toString().split(''); + const numberToGuessArr = numberToGuess.toString().split(''); + const result = { + bulls: 0, + cows: 0, + }; + + userInputArr.forEach((el, i) => { + if (el === numberToGuessArr[i]) { + result.bulls++; + } else if (numberToGuessArr.includes(el)) { + result.cows++; + } + }); + + return result; } module.exports = {