-
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
Solution #433
base: master
Are you sure you want to change the base?
Solution #433
Changes from 2 commits
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,34 @@ | ||
'use strict'; | ||
|
||
// Write your code here | ||
const readline = require('node:readline'); | ||
const { stdin, stdout, exit } = require('node:process'); | ||
|
||
const { generateRandomNumber } = require('./modules/generateRandomNumber'); | ||
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput'); | ||
const { getBullsAndCows } = require('./modules/getBullsAndCows'); | ||
|
||
const PROMPT_MESSAGE = 'Write 4 digits: \n'; | ||
const INVALID_INPUT_MESSAGE = 'The input must be 4 uniq digits: \n'; | ||
const TRY_AGAIN_MESSAGE = 'Need to try one more time: \n'; | ||
const WIN_MESSAGE = 'You win! The correct answer is'; | ||
|
||
const input = readline.createInterface({ input: stdin, output: stdout }); | ||
const answer = generateRandomNumber(); | ||
|
||
stdout.write(PROMPT_MESSAGE); | ||
|
||
input.on('line', (text) => { | ||
if (!checkIsValidUserInput(text)) { | ||
stdout.write(INVALID_INPUT_MESSAGE); | ||
} | ||
|
||
const { bulls, cows } = getBullsAndCows(text, answer); | ||
|
||
if (bulls === 4) { | ||
stdout.write(`${WIN_MESSAGE} ${text}`); | ||
input.close(); | ||
exit(); | ||
} | ||
|
||
stdout.write(`{bulls: ${bulls}, cows: ${cows}} \n${TRY_AGAIN_MESSAGE}`); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,17 @@ | |
* | ||
* @return {number} A random 4-digit number | ||
*/ | ||
|
||
function generateRandomNumber() { | ||
/* Write your code here */ | ||
const uniqRandomNumbers = new Set(); | ||
|
||
while (uniqRandomNumbers.size !== 4) { | ||
const randomNumber = Math.floor(1 + Math.random() * 9); | ||
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 for generating random numbers only produces digits from 1 to 9. This ensures no leading zero, but the logic for ensuring exactly 4 unique digits is flawed. Consider modifying the logic to ensure the first digit is non-zero and the remaining digits are unique, potentially by generating the first digit separately and then filling the rest. |
||
|
||
uniqRandomNumbers.add(randomNumber); | ||
} | ||
|
||
return Number(Array.from(uniqRandomNumbers).join('')); | ||
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 conversion of the set to a number may result in fewer than 4 digits if the set contains leading zeros. Ensure that the number is always 4 digits long by adjusting the logic to handle leading zeros appropriately. |
||
} | ||
|
||
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.
Currently, if the input is invalid, the program only writes an error message but does not return or prompt the user to input again. You should add a
return
statement after writing theINVALID_INPUT_MESSAGE
to prevent further execution of the code block and prompt the user to try again.