Number guesser problem with compareGuesses function

Hi Everyone!
I have a slight problem with https://www.codecademy.com/practice/projects/number-guesser-independent-practice, particularly with compareGuesses function.
This is what use, but for some reason, the program doesn’t determine the right winner, always say that the user won.

function compareGuesses (humanGuess, computerGuess, targetGuess) {
  var humanDifference =  Math.abs(targetGuess - humanGuess);
  var computerDifference = Math.abs(targetGuess - computerGuess);
  if (humanDifference > computerDifference) {
    return computerDifference;
  }
  if (humanDifference < computerDifference) {
    return humanDifference;
  }
  if (humanDifference === computerDifference) {
    return humanDifference;
  }
}

Also, I don’t really get what is the meaning of this code, how is it supposed to return who is closer to the random number. And when I put this code, the program works.

return humanDifference <= computerDifference;

looking at the requirements:

Return true if the human player wins, and false if the computer player wins

you need to return a Boolean value, not a a number.

as for:

return humanDifference <= computerDifference;

this comparison results in a Boolean value, which means the human (true) or computer (false) has won.

3 Likes

Thanks for your help and the explanation! It worked like this:

function compareGuesses (humanGuess, computerGuess, targetGuess) {
  var humanDifference =  Math.abs(targetGuess - humanGuess);
  var computerDifference = Math.abs(targetGuess - computerGuess);
  if (humanDifference > computerDifference) {
    return false;
  }
  if (humanDifference < computerDifference) {
    return true;
  }
  if (humanDifference === computerDifference) {
    return true;
  }
}

As for:

return humanDifference <= computerDifference;

can you suggest some reading material so that I can better understand this.

a comparison always results in a Boolean value, given a comparison is either true or false, that is all that there is to it:

console.log(5 === 5);
console.log(typeof(5 === 5));
3 Likes