I need help with the Number Guesser Game, I'm stuck on step 4 (Full-Stack Engineer path, Module 3)

So I created a separate file to test out if my functions were working so far. I am so stuck it isn’t even funny. Despite my best attempts, it seems the program is selecting true or false at random but I feel like my instructions aren’t that open to interpretation. I used the arguments 1 for user and 9 for computer to test it out, but any number from 0-9 will do I suppose. Is my compareGuesses function using the wrong syntax or is this an instance where isolating script.js is creating this glitch? Some help would be appreciated

const generateTarget = () => Math.floor(Math.random() *10); const compareGuesses = (user, computer, target) => { let userDif = Math.abs(target - user); let compDif = Math.abs(target - computer); if (userDif < compDif) { return true; } else if (userDif === compDif) { return true; } else if (userDif > compDif) { return false; }; }; console.log(compareGuesses(1, 9, generateTarget())); console.log(generateTarget())
  if (userDif < compDif) {
    return true;
  } else if (userDif === compDif) {
    return true;
  } else if (userDif > compDif) {
    return false;
  };

Can we simplify that? Yes, especially since we are using return.

  if (userDif <= compDif) {
    return true;
  }
  return false;

Thanks for that tip, noted. However even with simplifying like so I’m still getting false readings.
here’s my current example:
I just ran the program, generateTarget() randomly gave me 6
user has picked 1, computer has picked 9, same as before
1 is 5 away from 6, 9 is only 3, so that means userDif > compDif hence we SHOULD get false
however right now as I’m looking at it I’m getting true
and just to double make sure I ran the program again repeatedly until I got 6 via generateTarget()
and now it’s saying false
how am I getting two different answers for the same argument?
I just don’t see what’s wrong with my code that’s causing this confusion.

actually nvm I realize what I’m doing wrong. I am running the console log twice and thus getting two separate answers. just because my 2nd log says 6 doesn’t mean 6 is what my first got. I need to run specific numbers as my target parameter to test if they’ll get a true or false, and doing so shows that my code is right I just confused myself with a redundant use of console.log.

1 Like