[Project Number Guesser]

Hi there,

I’m doing the Number Guesser Project and I’m already stuck on Step 4 :frowning:
I am trying to compare the guesses of the human and the computer, but seem to have an error in my Math.abs object. When I log the variables the maths is not right. What am I doing wrong here?

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;

// Write your code below:
const generateTarget = () => {
return Math.floor(Math.random() * 10);}

const targetNumber = generateTarget()
console.log(generateTarget());

const compareGuesses = (humanGuess,computerGuess, targetNumber) => {
  const humanWin = Math.abs(targetNumber - humanGuess); 
  const computerWin = Math.abs(targetNumber - computerGuess); 
  console.log(humanWin);
  console.log(computerWin);
  if (humanWin === computerWin) {
  return true};
  if (humanWin <= computerWin) {
    return true
  };
  if (humanWin >= computerWin) {
    return false
  };
};

console.log(compareGuesses(9,1,targetNumber));

The generateTarget() function is called twice in this code.

  • Once when you set targetNumber, then again when you console.log it.
  • That produces 2 distinct numbers.

Really you should console.log targetNumber instead of generateTarget() if you want to have an accurate representation of what’s going into your parameter.

This is kind of an error-checking technique: check the parameters before assuming that the method/function is not working. Although in the case of existing methods/functions, it never hurts to check MDN before troubleshooting:

2 Likes

Thank you, that makes total sense!
I didn’t question the Math.abs() method per se, I just thought I implemented it wrong somehow. :crazy_face:
The solution is actually super clear, I’m really bummed that I keep making these stupid mistakes.
Thanks a lot again!

Oh this isn’t a stupid mistake! It’s easy to get these things mixed up.

1 Like