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 #419

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Solution #419

wants to merge 2 commits into from

Conversation

ogerenko
Copy link

@ogerenko ogerenko commented Oct 9, 2024

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 your code so far! 🚀 However, there's a potential performance issue that needs your attention. The use of array method includes inside a forEach loop can lead to quadratic complexity, which may cause problems for larger inputs. Try to optimize this part. Don't worry, you're doing well, and you're very close to the finish line! Keep it up! 😊

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


function playGame(question = '') {
terminal.question(question, (userInput) => {
if (!checkIsValidUserInput(userInput)) {

Choose a reason for hiding this comment

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

Code quality issue: It's a good practice to keep your functions small and focused on doing one thing. The playGame function is doing multiple things: validating the user input, incrementing the count, getting the result, logging the result, checking the result, and deciding what to do next. Consider breaking it down into smaller functions to improve readability and maintainability.

console.log(
'It is not valid number! Input the 4 dight number, without duplicates!',
);
playGame('Try again!');

Choose a reason for hiding this comment

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

Bug: The 'Try again!' message is not very informative. It would be better to tell the user exactly what they did wrong and how they can correct it.


const result = getBullsAndCows(userInput, guessNumber);

console.log(result, '(Try', count, ')');

Choose a reason for hiding this comment

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

Code quality issue: The way you are logging the result is a bit confusing. It would be better to use a template string to make it clearer. For example: console.log(${result} (Try ${count}));


if (result.bulls === 4) {
console.log(
'Congratulation!!! You win!!! Number is... ',

Choose a reason for hiding this comment

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

Code quality issue: Consider using a template string for the win message as well. For example: console.log(Congratulation!!! You win!!! Number is... ${guessNumber} [From ${count} try!]);

} else {
const { bulls, cows } = result;

if (bulls === 0 && cows === 0) {

Choose a reason for hiding this comment

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

Recommendation: The conditions on lines 38, 42, and 46 are a bit confusing. It would be clearer to use a switch statement or an if-else-if chain instead. This would also allow you to remove the redundant conditions on lines 47-51.

const availableNums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const result = [];

const getRandomN = () => Math.floor(Math.random() * availableNums.length);

Choose a reason for hiding this comment

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

It's better to use more descriptive function names. getRandomN doesn't give a clear understanding of what this function does. Consider renaming it to something like getRandomIndex.


const getRandomN = () => Math.floor(Math.random() * availableNums.length);

result.push(availableNums.splice(getRandomN(), 1)[0]);

Choose a reason for hiding this comment

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

The use of splice method here can be confusing. It's better to avoid changing the original array. Consider using a different approach to select a random element from the array without modifying it.

const getRandomN = () => Math.floor(Math.random() * availableNums.length);

result.push(availableNums.splice(getRandomN(), 1)[0]);
availableNums.unshift(0);

Choose a reason for hiding this comment

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

The use of unshift here is not clear. If you need to add 0 to the list of available numbers, why not include it in the original array? Also, it's not clear why 0 is added after the first number is selected. This could lead to confusion for other developers reading your code.

result.push(availableNums.splice(getRandomN(), 1)[0]);
}

return Number(result.join(''));

Choose a reason for hiding this comment

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

Converting an array of numbers to a string and then back to a number can be unnecessary and may lead to confusion. Consider returning the array itself or joining the array elements into a string and returning that string.

Comment on lines +23 to +29
userInputArr.forEach((el, i) => {
if (el === numberToGuessArr[i]) {
result.bulls++;
} else if (numberToGuessArr.includes(el)) {
result.cows++;
}
});

Choose a reason for hiding this comment

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

Consider using a different strategy to improve the performance of this code. For instance, you could create a map (an object in JavaScript) to store the count of each digit in numberToGuessArr. Then, for each digit in userInputArr, you can check if it's in the map and update the counts of bulls and cows accordingly. This will reduce the time complexity to linear.

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