 # Number Guesser Project - Why does 1 function work but the other doesn't?

Hey there,

I’m working on the Number Guesser project currently. I’m trying to code the `compareGuesses` function and I can’t figure out why it won’t work. I was able to create what feels like a longer means of writing the function so it works but I can’t find out why my initial try didn’t work as well - it wouldn’t give me any errors, it just logged ‘false’ no matter what.

Here’s the working code:

``````const generateTarget = () => Math.floor(Math.random() * 10);

let targetNumber = generateTarget();
let humanGuess = 8;
let computerGuess = 9;
let humanRange = Math.abs(humanGuess - targetNumber);
let computerRange = Math.abs(computerGuess - targetNumber);

const compareGuesses =  (humanGuess, computerGuess, targetNumber) => {
if ((humanRange === computerRange) || (humanRange < computerRange)) {
return true;
} else {
return false;
}
}
``````

Here’s the broken code where I can’t understand why it’s broken:

``````const generateTarget = () => Math.floor(Math.random() * 10);

let targetNumber = generateTarget();
let humanGuess = 8;
let computerGuess = 9;

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

console.log(compareGuesses());
``````

Some more things to help with! So, I originally didn’t use the:

``````let humanGuess =
let computerGuess =
``````

and I was just doing a `console.log(compareGuesses(9, 8, );`. It felt wrong to have the empty parameter but, since I already defined it with let `targetNumber = generateTarget();` it didn’t seem like I should also be defining it in the console.log(). How would I do this?

Last question:

I tried to define `let targetNumber = generateTarget();` inside the `compareGuesses()` function but before the if statement and it sent back an error. Any ideas why? Is it proper to define targetNumber above the function?

I really appreciate any time you spend in helping me understand this and, from a new coder, the extra help on the forum is SO helpful. So, thank you in advance!

UPDATE (kind of):

So I still haven’t answered any of those above questions but I did continue going through the exercise with the working code and…it doesn’t work! I’ve searched through other people’s solutions and I have no clue why there’s would work and mine wouldn’t. Please help! Here is my full script.js code:

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

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

let targetNumber = generateTarget();
let humanRange = Math.abs(humanGuess - targetNumber);
let computerRange = Math.abs(computerGuess - targetNumber);

const compareGuesses =  (humanGuess, computerGuess, targetNumber) => {
if ((humanRange === computerRange) || (humanRange < computerRange)) {
return true;
} else {
return false;
}
}

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

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

Well, as long as no one has solved this. I’ll continue adding to the confusion. When I include

``````let humanGuess = 5;
let computerGuess = generateTarget();
``````

in my code (in between the `let targetNumber = generateTarget()` and `let humanRange = Math.abs(humansGuess - targetNumber);`). The actual UX/UI in my browser works but when I remove those two lines of code, there’s no reaction to me pressing Make a Guess.

Stick to the instructions given to you for the project. You are not required to declare variables globally or call functions. There is a second JavaScript file that will take care of those things for you.

1 Like

Could you be more specific?

The instructions never tell you to call `generateTarget` or to declare the variables `targetNumber`, `humanRange` and `computerRange`, simply to have parameters of the `compareGuesses` function where inside you can declare them for cleaner looking code but not necessary. All you need to do for this project is write the functions.

2 Likes