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

add task solution #429

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

add task solution #429

wants to merge 2 commits into from

Conversation

P-Nazar
Copy link

@P-Nazar P-Nazar commented Nov 1, 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.

Thank you for your hard work on this project! While your code meets many of the task requirements, there are a few critical issues that need to be addressed before it can be approved.

  1. Handling Invalid Input: The use of recursion for handling invalid input can lead to a stack overflow if the user continuously inputs invalid data. Switching to a loop will prevent this issue and improve the robustness of your code.

  2. Validation Logic: The current logic for checking duplicate digits using a Set is flawed. The Set is created from the entire input string, which does not correctly check for duplicates. Consider splitting the input into individual characters before creating a Set.

  3. Order of Digits: When generating a random number, ensure that the order of digits is preserved, especially to prevent the first digit from being zero. The current method of converting a Set to an array may not maintain the intended order.

  4. Cows Calculation: The logic for counting cows should be adjusted to ensure that digits already counted as bulls are not counted again as cows.

By addressing these issues, your code will be more reliable and maintainable. Keep up the great work, and I'm confident you'll nail it! 😊

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

🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review button in the Reviewers tab.


if (!validInput) {
print('невалідне значення');
playRound();

Choose a reason for hiding this comment

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

Using recursion for handling invalid input can lead to a stack overflow if the user continuously inputs invalid data. Consider using a loop to handle repeated attempts instead of recursion.

@@ -9,7 +9,22 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
const validInput = new Set(userInput);

Choose a reason for hiding this comment

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

The use of Set to check for duplicate digits is incorrect here. The Set is created from the entire input string, which does not correctly check for duplicates. Consider splitting the input into individual characters and then creating a Set to check for duplicates.

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 check for whether the input is a number should be done before checking the size of the Set to ensure the input is numeric.

digits.add(digit);
}

return parseInt(Array.from(digits).join(''));

Choose a reason for hiding this comment

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

Converting a Set to an array and then joining the digits may not preserve the order of digits as intended. Consider using a different approach to ensure the digits are ordered correctly, especially ensuring the first digit is not zero.

Comment on lines 27 to 28
} else if (numberToGuessStr.includes(userInputStr[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 logic for counting cows does not account for cases where a digit in the user input has already been counted as a bull. Consider adding a mechanism to ensure that a digit is not counted as a cow if it has already been counted as a bull.

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 command-line game! 🎉 Your code meets the task requirements and constraints, and you've done well to modularize the functions for random number generation, input validation, and bulls and cows calculation.

However, there are a couple of areas for improvement:

  1. Cows Calculation Logic: There's a logical error in the getBullsAndCows function. The current implementation may incorrectly count cows because it checks for the presence of a digit without ensuring that the position is not already a bull. Make sure to verify that the cowIndex is not equal to i and that checkedPositions[cowIndex] is false before incrementing the cow count.

  2. Redundant Function Call: In your main game logic, the variable validInput is assigned the result of checkIsValidUserInput(guess), but the function is called again in the if condition. You can optimize this by using validInput directly in the condition to avoid redundant function calls.

These adjustments will enhance the accuracy and efficiency of your code. Keep up the good work, and continue to learn from these insights to improve your coding skills! 🚀

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

Comment on lines +20 to +22
const validInput = checkIsValidUserInput(guess);

if (!checkIsValidUserInput(guess)) {

Choose a reason for hiding this comment

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

The variable validInput is assigned the result of checkIsValidUserInput(guess), but the same function is called again in the if condition on line 22. You can use validInput directly in the condition to avoid redundant function calls.

Comment on lines +35 to +39
const cowIndex = numberToGuessStr.indexOf(userInputStr[i]);

if (cowIndex !== -1 && !checkedPositions[cowIndex]) {
result.cows++;
checkedPositions[cowIndex] = true;

Choose a reason for hiding this comment

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

The logic for calculating cows is incorrect. The current implementation checks for the presence of a digit in the numberToGuessStr but does not ensure that the cowIndex is not already a bull. This can lead to incorrect cow counts. Consider checking if cowIndex is not equal to i and that checkedPositions[cowIndex] is false before incrementing the cow count.

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