Skip to content

Commit

Permalink
fix points
Browse files Browse the repository at this point in the history
  • Loading branch information
ly-manka committed Nov 12, 2024
1 parent 25fd243 commit af05c51
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 12 additions & 14 deletions src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit af05c51

Please sign in to comment.