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 all commits
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
7 changes: 6 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
const isFourDigits = userInput.length === 4;
const hasUniqueDigits = new Set(userInput.split('')).size === 4;
const isNumber = !isNaN(+userInput);

Choose a reason for hiding this comment

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

The check isNumber using !isNaN(+userInput) might not catch all invalid cases, such as strings with non-digit characters. Consider using a regular expression to ensure the input consists only of digits.

const doesNotStartWithZero = userInput[0] !== '0';

return isFourDigits && hasUniqueDigits && isNumber && doesNotStartWithZero;
}

module.exports = {
Expand Down
19 changes: 18 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
/* Write your code here */
const digits = new Array(4);

digits[0] = Math.floor(1 + Math.random() * 9);

const usedDigits = new Set([digits[0]]);

for (let i = 1; i < 4; i++) {
let randomDigit;

do {
randomDigit = Math.floor(Math.random() * 10);
} while (usedDigits.has(randomDigit));

digits[i] = randomDigit;
usedDigits.add(randomDigit);
}

return Number(digits.join(''));
}

module.exports = {
Expand Down
18 changes: 17 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
const result = {
bulls: 0,
cows: 0,
};

const userNumber = userInput.toString();
const guessNumber = numberToGuess.toString();

for (let i = 0; i < userNumber.length; i++) {
if (userNumber[i] === guessNumber[i]) {
result.bulls++;
} else if (guessNumber.includes(userNumber[i])) {
result.cows++;
Comment on lines +27 to +28

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 may overcount if a digit appears more than once in the numberToGuess. Implement a mechanism to track which digits have already been counted as cows to ensure accuracy.

}
}

return result;
}

module.exports = {
Expand Down
Loading