Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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):

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.

return false;

case new Set(userInput.split('')).size !== 4:
return false;

default:
return true;
}

Choose a reason for hiding this comment

The 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 if statements might be more readable for this type of validation logic. Consider refactoring for clarity.

}

module.exports = {
Expand Down
14 changes: 13 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(''));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation converts the Set of digits directly to a number. This approach does not guarantee that the resulting number will always be a valid 4-digit number, as the order of digits in a Set is not controlled. Consider using an array to manage the order of digits explicitly, ensuring the first digit remains at the start.

}

module.exports = {
Expand Down
22 changes: 21 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The 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 numberToGuessStr. Consider using a mechanism to track which digits have already been counted as cows to avoid overcounting.

) {
cows += 1;
}
}

return { bulls, cows };
}

module.exports = {
Expand Down
Loading