Number Guesser help



I’ve been working on the number guesser project for a little while and I’ve got pretty much everything figured out except part 4.

Nothing is returning an error message, but I find that I either always win or always loose when I run my code:

const compareGuesses = (userInput, computerInput, targetNumber) => {

if (computerInput === userInput) {
    return true
} else if (computerInput > userInput > targetNumber) {
    return true
} else if (computerInput < userInput < targetNumber) {
    return true
} else {
    return false


I tried to figure it out for a while before I looked at the solution:

const compareGuesses = (humanGuess, computerGuess, targetGuess) => {
const humanDifference = Math.abs(targetGuess - humanGuess)
const computerDifference = Math.abs(targetGuess - computerGuess)
return humanDifference <= computerDifference;

I understand why the solution works, but I’m trying to figure out why mine did not. As far as I can tell it should return true or false depending on if the userInput is closer to the targetNumber regardless of absolute values since if the computer number is closer you should loose, but if you are closer or tied you should win.

Thanks for any help!

Hello, @lophane, and welcome to the forums.

Comparisons like these do not work in JavaScript:

You would need if(computerInput > userInput && userInput > targetNumber) for an equivalent comparison.


Thank you!

I hadn’t realized that you cant have multiple comparisons like that, thanks for the help!

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.