-
Notifications
You must be signed in to change notification settings - Fork 538
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
bulls and cows #440
base: master
Are you sure you want to change the base?
bulls and cows #440
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 |
---|---|---|
|
@@ -9,7 +9,19 @@ | |
* @return {boolean} - True if the user input is valid, false otherwise | ||
*/ | ||
function checkIsValidUserInput(userInput) { | ||
/* Write your code here */ | ||
switch (true) { | ||
case userInput[0] === '0': | ||
return false; | ||
|
||
case isNaN(+userInput): | ||
return false; | ||
|
||
case new Set(userInput.split('')).size !== 4: | ||
return false; | ||
|
||
default: | ||
return true; | ||
} | ||
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 switch statement is used here to validate the user input. While it works, using a series of |
||
} | ||
|
||
module.exports = { | ||
|
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 = new Set(); | ||
|
||
const firstDigit = Math.floor(1 + Math.random() * 9); | ||
|
||
digits.add(firstDigit); | ||
|
||
while (digits.size < 4) { | ||
const randomDigit = Math.floor(Math.random() * 10); | ||
|
||
digits.add(randomDigit); | ||
} | ||
|
||
return Number(Array.from(digits).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 current implementation converts the |
||
} | ||
|
||
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 */ | ||
let bulls = 0; | ||
let cows = 0; | ||
|
||
const userInputStr = String(userInput); | ||
const numberToGuessStr = String(numberToGuess); | ||
|
||
for (let i = 0; i < userInputStr.length; i++) { | ||
const guessNumber = userInputStr[i]; | ||
const rightNumber = numberToGuessStr[i]; | ||
|
||
if (guessNumber === rightNumber) { | ||
bulls += 1; | ||
} else if ( | ||
numberToGuessStr.includes(guessNumber) && | ||
guessNumber !== rightNumber | ||
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 does not account for the possibility of counting the same digit multiple times if it appears more than once in the |
||
) { | ||
cows += 1; | ||
} | ||
} | ||
|
||
return { bulls, cows }; | ||
} | ||
|
||
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 check
isNaN(+userInput)
is intended to ensure the input is numeric. However, this will not catch cases where the input is a numeric string with non-digit characters (e.g., '12a4'). Consider using a regular expression to ensure the input is strictly a 4-digit number.