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

Solution #417

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Solution #417

wants to merge 1 commit into from

Conversation

RuslanSkl
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Great job on implementing the core functionality of the getBullsAndCows function! 👍 The game logic appears to be working as expected. Just remember to keep an eye on the minor suggestions such as refining input validation and considering alternative coding styles for future improvements. These can enhance the readability and robustness of your code. Keep coding and refining your skills! 🚀

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

const { getBullsAndCows } = require('./modules/getBullsAndCows.js');
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput.js');

const numberToGues = generateRandomNumber();

Choose a reason for hiding this comment

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

There is a typo in the variable name 'numberToGues'. It should be 'numberToGuess' to correctly reflect its purpose.

Comment on lines +16 to +31
function question() {
rl.question('What is your number?', (userAnswer) => {
if (!checkIsValidUserInput(userAnswer)) {
console.log('Number must include 4 unique digits');

return question();
}

const result = getBullsAndCows(userAnswer, numberToGues);

console.log(
`Your number: ${userAnswer} ||`,
`Random number: ${numberToGues}`,
result,
);
rl.close();

Choose a reason for hiding this comment

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

The game does not continue after a valid guess is made. You should only close the readline interface when the user has guessed the correct number (all bulls). Otherwise, the game should continue by calling question() again.

`Random number: ${numberToGues}`,
result,
);
rl.close();

Choose a reason for hiding this comment

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

After printing the result, the game ends regardless of whether the player won or lost. You may want to add a condition to check if the user has won (e.g., all digits are bulls) before closing the readline interface.

Comment on lines +20 to +30
for (let i = 0; i < userInput.length; i++) {
const check = userInput.slice(1);

if (userInput[0] === '0') {
return false;
}

if (check.includes(userInput[0])) {
return false;
}
}

Choose a reason for hiding this comment

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

The current implementation of the loop and checks inside it may not correctly validate the input. The slice is being done on each iteration but only the first character of userInput is being compared and checked for duplicates. This means that only the first character is being validated for uniqueness and not being '0', which is incorrect. Instead, you should iterate over each character in userInput and check if it is unique within the string and not '0'.

return false;
}

if (isNaN(userInput)) {

Choose a reason for hiding this comment

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

The isNaN function can give incorrect results when used with strings that represent numbers. For example, isNaN('1234') will return false, which is correct, but isNaN('0123') will also return false, which may not be the intended behavior if leading zeros are not allowed. Consider using a regular expression to ensure that the input is a four-digit number with no leading zeros and each digit is unique.

Comment on lines 9 to +24
function generateRandomNumber() {
/* Write your code here */
let digits = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
let number = digits[Math.floor(Math.random() * digits.length)];

digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].filter(
(d) => !number.includes(d),
);

while (number.length < 4) {
const randomIndex = Math.floor(Math.random() * digits.length);

number += digits[randomIndex];
digits.splice(randomIndex, 1);
}

return Number(number);

Choose a reason for hiding this comment

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

The function generateRandomNumber generates a random number with unique digits, which is good. However, the current implementation has a potential issue. It always generates a number starting with a digit between 1 and 9, and then includes 0 in the subsequent digits. If the game rules allow the first digit to be 0, this function would not comply with the rules. Ensure the game rules are being followed correctly.

digits.splice(randomIndex, 1);
}

return Number(number);

Choose a reason for hiding this comment

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

The function returns the number as a Number type. If the first randomly generated digit is '0' after modifying the function to include '0' as a possible first digit, Number(number) will remove it, which may not be the desired outcome if the leading zero is significant. Consider returning the number as a string if leading zeroes are important.

Comment on lines +21 to +22
const userInputToString = userInput.toString().split('');
const numberToGuessToString = numberToGuess.toString().split('');

Choose a reason for hiding this comment

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

It's a good practice to ensure that the input parameters are of expected types before performing operations on them. Consider adding a type check for 'userInput' and 'numberToGuess' to ensure they are numbers and handle any potential type-related errors.

Comment on lines +21 to +22
const userInputToString = userInput.toString().split('');
const numberToGuessToString = numberToGuess.toString().split('');

Choose a reason for hiding this comment

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

You are converting both 'userInput' and 'numberToGuess' to strings and then splitting them into arrays. This is a valid approach, but to make the code more concise, you could perform these operations in a single step using Array.from, like so: Array.from(userInput.toString()).

Comment on lines +24 to +30
for (let i = 0; i < userInputToString.length; i++) {
if (userInputToString[i] === numberToGuessToString[i]) {
result.bulls++;
} else if (numberToGuessToString.includes(userInputToString[i])) {
result.cows++;
}
}

Choose a reason for hiding this comment

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

The current implementation of the loop is correct, but you might consider using a more functional approach with Array methods like 'forEach' for better readability. However, this is more of a stylistic choice and the current loop is perfectly acceptable if you prefer this style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants