-
Notifications
You must be signed in to change notification settings - Fork 543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add bulls and cows game #432
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,43 @@ | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
|
||
// Write your code here | ||
const readline = require('node:readline'); | ||
|
||
const { generateRandomNumber } = require('./modules/generateRandomNumber'); | ||
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput'); | ||
const { getBullsAndCows } = require('./modules/getBullsAndCows'); | ||
|
||
const randomNum = generateRandomNumber(); | ||
|
||
console.log('Random number:', randomNum); | ||
askQuestion(randomNum); | ||
|
||
function askQuestion(randomNumber) { | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
const promptUser = () => { | ||
rl.question('Guess the number:\n', (userNumber) => { | ||
if (checkIsValidUserInput(userNumber)) { | ||
const userTry = getBullsAndCows(userNumber, randomNumber); | ||
|
||
console.log(userTry); | ||
|
||
if (userTry.bulls === 4) { | ||
console.log('You win!'); | ||
rl.close(); | ||
} else { | ||
console.log('Try again'); | ||
promptUser(); | ||
} | ||
} else { | ||
console.log('Number should include 4 uniq digits from 1 to 9'); | ||
promptUser(); | ||
} | ||
}); | ||
}; | ||
|
||
promptUser(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,19 @@ | |
* @return {number} A random 4-digit number | ||
*/ | ||
function generateRandomNumber() { | ||
/* Write your code here */ | ||
const digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
const vailableDigits = [...digits]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the variable name |
||
let number = ''; | ||
|
||
for (let i = 0; i < 4; i++) { | ||
const randomIndex = Math.floor(Math.random() * vailableDigits.length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the typo in |
||
const randomDigit = vailableDigits[randomIndex]; | ||
|
||
number = number.concat(randomDigit); | ||
vailableDigits.splice(randomIndex, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the typo in |
||
} | ||
|
||
return Number(number); | ||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,27 @@ | |
* Example: { bulls: 1, cows: 2 } | ||
*/ | ||
function getBullsAndCows(userInput, numberToGuess) { | ||
/* Write your code here */ | ||
const bullsAndCows = { | ||
bulls: 0, | ||
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]; | ||
|
||
if (userNumbers.includes(number)) { | ||
if (userNumbers.indexOf(number) === i) { | ||
bullsAndCows.bulls += 1; | ||
} else { | ||
bullsAndCows.cows += 1; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current logic may incorrectly count a digit as a cow even if it has already been counted as a bull. Consider using separate loops or a mechanism to track which digits have been counted as bulls to prevent them from being counted as cows. |
||
} | ||
|
||
return bullsAndCows; | ||
} | ||
|
||
module.exports = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message could be more specific. Consider changing it to: 'Number should include 4 unique digits from 1 to 9.' This makes it clear that the digits must be unique and within the specified range.