-
Notifications
You must be signed in to change notification settings - Fork 537
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
Develop #428
base: master
Are you sure you want to change the base?
Develop #428
Changes from all 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,42 @@ | ||
'use strict'; | ||
|
||
// 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 numberToGuess = generateRandomNumber(); | ||
let isFirstAttempt = true; | ||
const print = (message) => terminal.write(message + '\n'); | ||
|
||
const askQuestion = () => { | ||
const prompt = isFirstAttempt | ||
? `I generated a number of 4 different digits. Please, guess a number :)\n` | ||
: 'Your guess:\n'; | ||
|
||
terminal.question(prompt, (userInput) => { | ||
isFirstAttempt = false; | ||
|
||
if (!checkIsValidUserInput(userInput)) { | ||
print('Your input is invalid, please, try again :)'); | ||
|
||
return askQuestion(); | ||
} | ||
|
||
const { bulls, cows } = getBullsAndCows(userInput, numberToGuess); | ||
|
||
print(`bulls: ${bulls}, cows: ${cows}\n`); | ||
|
||
if (bulls === 4) { | ||
print('Congratulations!'); | ||
|
||
return terminal.close(); | ||
} | ||
|
||
askQuestion(); | ||
}); | ||
}; | ||
|
||
askQuestion(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,21 @@ | |
* @return {number} A random 4-digit number | ||
*/ | ||
function generateRandomNumber() { | ||
/* Write your code here */ | ||
let resultStr = ''; | ||
|
||
while (resultStr.length < 4) { | ||
const randomDigitStr = String(Math.floor(Math.random() * 10)); | ||
|
||
if (resultStr.length === 0) { | ||
if (randomDigitStr !== '0') { | ||
resultStr += randomDigitStr; | ||
} | ||
} else if (!resultStr.includes(randomDigitStr)) { | ||
resultStr += randomDigitStr; | ||
} | ||
} | ||
|
||
return +resultStr; | ||
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 function returns a number by converting the string |
||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,25 @@ | |
* Example: { bulls: 1, cows: 2 } | ||
*/ | ||
function getBullsAndCows(userInput, numberToGuess) { | ||
/* Write your code here */ | ||
const numberToGuessStr = String(numberToGuess); | ||
const userInputStr = String(userInput); | ||
const numberToGuessStrArr = numberToGuessStr.split(''); | ||
const userInputStrArr = userInputStr.split(''); | ||
|
||
const result = { | ||
bulls: 0, | ||
cows: 0, | ||
}; | ||
|
||
for (let i = 0; i < userInputStrArr.length; i++) { | ||
if (userInputStrArr[i] === numberToGuessStrArr[i]) { | ||
result.bulls++; | ||
} else if (numberToGuessStr.includes(userInputStrArr[i])) { | ||
result.cows++; | ||
Comment on lines
+29
to
+30
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 counting cows might incorrectly count a digit as a cow if it appears multiple times in the user input, even if it has already been counted as a bull. Consider adding a mechanism to track which digits have already been matched to avoid counting them multiple times. |
||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
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 nested loop used to check for duplicate digits can be optimized. Currently, it checks each pair of digits twice, which is unnecessary. Consider using a Set to track seen digits, which can reduce the complexity and improve performance.