-
Notifications
You must be signed in to change notification settings - Fork 530
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
base: master
Are you sure you want to change the base?
add task solution #429
Conversation
There was a problem hiding this 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.
-
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.
-
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.
-
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.
-
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(); |
There was a problem hiding this comment.
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.
src/modules/checkIsValidUserInput.js
Outdated
@@ -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); |
There was a problem hiding this comment.
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.
src/modules/checkIsValidUserInput.js
Outdated
return false; | ||
} | ||
|
||
if (isNaN(+userInput)) { |
There was a problem hiding this comment.
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.
src/modules/generateRandomNumber.js
Outdated
digits.add(digit); | ||
} | ||
|
||
return parseInt(Array.from(digits).join('')); |
There was a problem hiding this comment.
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.
src/modules/getBullsAndCows.js
Outdated
} else if (numberToGuessStr.includes(userInputStr[i])) { | ||
result.cows++; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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:
-
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 thecowIndex
is not equal toi
and thatcheckedPositions[cowIndex]
is false before incrementing the cow count. -
Redundant Function Call: In your main game logic, the variable
validInput
is assigned the result ofcheckIsValidUserInput(guess)
, but the function is called again in the if condition. You can optimize this by usingvalidInput
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! ✨
const validInput = checkIsValidUserInput(guess); | ||
|
||
if (!checkIsValidUserInput(guess)) { |
There was a problem hiding this comment.
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.
const cowIndex = numberToGuessStr.indexOf(userInputStr[i]); | ||
|
||
if (cowIndex !== -1 && !checkedPositions[cowIndex]) { | ||
result.cows++; | ||
checkedPositions[cowIndex] = true; |
There was a problem hiding this comment.
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.
No description provided.