 # Function isn't working properly in Number Guesser project

Im having trouble with task 4 on the Number Guesser Project. My compareGuesses function isn’t working properly.
I tried to find the difference between the secretTarget & computerScore and secretTarget & humanScore to calculate the values for each of the parameters (humanScore, computerScore, secretTarget).
Then wrote an if statement that returns true if the computerScore and humanScore are equal and also returns true if humanScore is less than computerScore. False for everything else.

With that being said, why is it that if the
computerScore = 9 [difference = 5]
humanScore = 1 [difference = 3]
secretTarget = 4
the computer wins??

From what I think I wrote, the difference between humanScore and secretTarget is less than the difference between the computerScore and the secretTarget… So shouldn’t the human win?

This is my code:

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

const generateTarget = () => {
return Math.floor(Math.random() * 10);
}

const compareGuesses = (humanScore, computerScore, secretTarget) => {
secretTarget = generateTarget();
humanScore = Math.abs(secretTarget - humanScore);
computerScore = Math.abs(secretTarget - computerScore);

if (humanScore === computerScore) {
return true;
}else if (humanScore < computerScore) {
return true;
}else {
return false;
}
}

const updateScore = winner => {
//winner = “”;
if (winner === ‘human’) {
humanScore++;
}else if (winner == “computer”) {
computerScore++;
}
};

const advanceRound = () => {
currentRoundNumber++;
}

Hi there! For future reference, this post might helpful: How do I format code in my posts?

I’m a little lost. I don’t see in any part of your code a call to `compareGuesses()` so how is your code playing the game? And why is `compareGuesses()` defined with 3 parameters but then calculate those parameters inside the function? Wouldn’t it make more sense to have a function that “plays” the game generating all three `secreTarget`, `humanScore`, and `computerScore` and calls `compareGuesses()` with those arguments?

I also don’t see any calls to your function `updateScore` so how are you counting who wins?

hey there thanks for the reply!

so upon re-reading it i noticed a few errors and changed the variable names in the compareGuesses function.
the game still does weird errors where the wrong person wins… for example

Human score: 6
Computer score: 4
Target Number: 2
YOU WIN!! but shouldn’t the computer win?

CODE BELOW:

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

const generateTarget = () => {
return Math.floor(Math.random() * 10);
}

const compareGuesses = (humanGuess, computerGuess, secretTarget) => {
secretTarget = generateTarget();
humanGuess = Math.abs(secretTarget - humanGuess);
computerGuess = Math.abs(secretTarget - computerGuess);

if (humanGuess === computerGuess) {
return true;
}else if (humanGuess < computerGuess) {
return true;
}else {
return false;
}
}

const updateScore = winner => {
//winner = " ";
if (winner === 'human') {
humanScore++;
}else if (winner === 'computer') {
computerScore++;
}
}

const advanceRound = () => {
return currentRoundNumber++;
}

compareGuesses();
``````

Looking at your code I noticed this in your `compareGuesses()` function:

``````humanGuess = Math.abs(secretTarget - humanGuess);
computerGuess = Math.abs(secretTarget - computerGuess);
``````

it seems you are defining and using `humanGuess` and `computerGuess` at the same time?

Yes…? I guess I am. Is that wrong? Should i need to re-write that?

well, think about the logic. You define humanGuess as the absolute of `secretTarget - humanGuess`, but you haven’t defined `humanGuess` yet, so it has no value. If you try to `console.log(humanGuess)` at that point, it’s going to be equal to `secretTarget`. And the same goes for `computerGuess`.

That means that `if (humanGuess === computerGuess)` will be true and your function will return true.

What I still don’t understand is when do you call your function `updateScore` ?