Number Guesser Game compareGuess() function

Hello all, I have been sitting here trying to get around how the compareGuess() function is supposed to work and I cant wrap my head around it and no matter any way I look at it nothing I come up with works. So far I assume that Math.abs is neccesary but I find that when I test this out on paper I get a return statement of true even if the human is not the winner. Here is what I have so far, though I know it wont work.

function compareGuess(humanGuess, computerGuess, targetGuess) => {
if (Math.abs(humanGuess-targetGues())) > Math.abs(computerGuess - targetGuess()) {
return true
}
Here is the link to the exercise
https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-challenge-project-number-guesser/projects/number-guesser-independent-practice

Hi, I’ll try and simplify how compareGuess() is supposed to work (or, more accurately, how I made it work) and hopefully it’ll help:

So compareGuess() takes 3 variables. The human guess, the computer and the target number. If the user guess is closer or of equal distance to the target number than the computer guess, the user wins. If the computer guess is closer to the target number, the computer wins.
A good way to achieve this result is similar to yours, checking the difference between target num and each guess, rounding them to a whole number and seeing which one is greater than the other, which should determine the winner correctly.

Hope it helps! If you’re still struggling let me know and I’ll post the solution.

1 Like

For your function compare you have to do clean up the function compareGuess and as well you have to switch “function compareGuess” to “const compareGuess” (the const variable will let the function remain the same throughout the project):

first you have “())” which is stopping the function from performing correctly. the Math.abs will close with one “)” and the second “)” will be at the end of the second equation. having “())” means you have to insert another variable when you’re trying to close the equation. as well i reformatted the equation whenever the human or computer guesses are subtracted from the secret target, it will randomize better. so so it should read:

const compareGuess = (humanGuess, computerGuess, targetGuess) =>
 {
if (Math.abs(targetGuess - humanGuess) <  Math.abs(targetGuess - computerGuess))
 {
 return true;
}
}

2 Likes

Hi, thank you for this. It took me roughly 30 minutes of looking at it to see where I went wrong in my initial code. THANK YOU IT’S FINALLY MAKING A LITTLE MORE SENSE. GOD BLESS YOU

1 Like