From af05c516866e8c8d51f2d1d0b161eaaa4e1df152 Mon Sep 17 00:00:00 2001 From: Iryna Lysko Date: Tue, 12 Nov 2024 20:45:03 +0200 Subject: [PATCH] fix points --- src/app.js | 2 +- src/modules/generateRandomNumber.js | 8 ++++---- src/modules/getBullsAndCows.js | 26 ++++++++++++-------------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/app.js b/src/app.js index 487eed1..f5b26b8 100644 --- a/src/app.js +++ b/src/app.js @@ -33,7 +33,7 @@ function askQuestion(randomNumber) { promptUser(); } } else { - console.log('Number should include 4 uniq digits from 1 to 9'); + console.log('Number should include 4 unique digits from 1 to 9'); promptUser(); } }); diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index a79964e..1d6b9b4 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -8,15 +8,15 @@ */ function generateRandomNumber() { const digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - const vailableDigits = [...digits]; + const availableDigits = [...digits]; let number = ''; for (let i = 0; i < 4; i++) { - const randomIndex = Math.floor(Math.random() * vailableDigits.length); - const randomDigit = vailableDigits[randomIndex]; + const randomIndex = Math.floor(Math.random() * availableDigits.length); + const randomDigit = availableDigits[randomIndex]; number = number.concat(randomDigit); - vailableDigits.splice(randomIndex, 1); + availableDigits.splice(randomIndex, 1); } return Number(number); diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 647300e..7844e21 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -13,27 +13,25 @@ * Example: { bulls: 1, cows: 2 } */ function getBullsAndCows(userInput, numberToGuess) { - const bullsAndCows = { - bulls: 0, - cows: 0, - }; + let bulls = 0; + let cows = 0; const userNumbers = Array.from(String(userInput), (num) => +num); const gameNumbers = Array.from(String(numberToGuess), (num) => +num); - for (let i = 0; i < 4; i++) { - const number = gameNumbers[i]; + gameNumbers.forEach((n, ind) => { + if (n === userNumbers[ind]) { + bulls += 1; + } + }); - if (userNumbers.includes(number)) { - if (userNumbers.indexOf(number) === i) { - bullsAndCows.bulls += 1; - } else { - bullsAndCows.cows += 1; - } + gameNumbers.forEach((n, ind) => { + if (userNumbers.includes(n) && ind !== userNumbers.indexOf(n)) { + cows += 1; } - } + }); - return bullsAndCows; + return { bulls, cows }; } module.exports = {