 # Number Guesser

Hi everyone, lately I’ve been trying to create a solution for the Number Guesser challenge. I managed to write a code which I think is close to the final result but still can’t figure out what mistakes I have made. The code looks like this

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

const targetGuess = Math.floor(Math.random() *9)

const compareGuesses = (humanGuess, computerGuess, targetGuess) => {
if (math.abs (humanGuess - targetGuess) < math.abs(computerGuess - targetGuess)) {
return true
}else if (math.abs (humanGuess - targetGuess) > math.abs(computerGuess - targetGuess)) {
return false
}else {
return true
}
}
const updateScore = (winner) => {
if (compareGuesses = true) {
let humanScore = humanScore+1
}else if (compareGuesses = false) {
let computerScore = computerScore+1
}
}

const advanceRound = (currentRoundNumber) => {
if (compareGuesses = true) {
let  currentRoundNumber = currentRoundNumber+1
} else if (compareGuesses = false) {
}
``````

I have some issues regarding some of the tasks included in the challenge because, as long as it is going to run in a html/CSS website … the instructions told me from the very beginning that `As you complete this project, make sure that all of your functions are named exactly as specified so that they can be called correctly when the game is played`. Things being said, I thought that there is no freestyle regarding this code because I have to use some specific values… and I wasn’t told to create a value for the computerGuess or the humanGuess so… idk where I failed to complete the code

here is the link for the challenge

1 Like

The most obvious problem is that, whenever you’re checking if two values(or variables) are equal, you are using `variable = value`, when in order to check if two values are equal, you should use `==`:
`variable == value`, but you can also(and should) use `===`.
The difference is that `==` checks if two values are equal, while `===` also checks if they’re the same type, and not just equal:

``````let boolean = true;
let string = "true";
let integer = 1;
boolean == string // true
boolean == integer // true, that's because 1 means true, and 0 means false, like in binary
boolean === string // false
boolean === integer // false
``````

The second problem is that:

`else if` isn’t closed, but I assume it got cut off.
I hope this helps i modified the code as you suggested but still it won’t run, now it looks like this

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

const targetGuess = Math.floor(Math.random() *9)

const compareGuesses = (humanGuess, computerGuess, targetGuess) => {
if (math.abs (humanGuess - targetGuess) < math.abs(computerGuess - targetGuess)) {
return true
}else if (math.abs (humanGuess - targetGuess) > math.abs(computerGuess - targetGuess)) {
return false
}else {
return true
}
}
const updateScore = (winner) => {
if (compareGuesses === true) {
let humanScore = humanScore+1
}else if (compareGuesses === false) {
let computerScore = computerScore+1
}
}

const advanceRound = (currentRoundNumber) => {
if (compareGuesses === true) {
let  currentRoundNumber = currentRoundNumber+1
} else if (compareGuesses === false) {
let currentRoundNumber = currentRoundNumber+1
}
}
``````

any other suggestions?
thanks

1 Like

also it still does not work

1 Like

It seems you haven’t created a `generateTarget()` function, so take a look at the 3rd task/objective.
Also `updateScore` doesn’t use `compareGuesses` to determine who wins, you have to use the winner parameter, read the 5th task/objective.

Keep in mind that you have to write the functions, but you’re not writing the game itself.
You’re just providing the game functions for it to work. Thus you can’t use your own functions inside your other functions because you don’t know the things(parameters) for it to work.

This is how my code is working. The game is working except for counter for score is always ading +1 to the player, even if computer wins.

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

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

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

const updateScore = (winner) => {
if (compareGuesses() === true ) {
humanScore = humanScore + 1;
} else if  (compareGuesses() === false )
{ computerScore = computerScore + 1;
};
};
const advanceRound = (currentRoundNumber) => {
if (compareGuesses() === true) {
currentRoundNumber = currentRoundNumber + 1;
} else {
currentRoundNumber = currentRoundNumber +1;
} ;
};
``````

Hi there, welcome to the forums.

I’m going to guess it’s because of the way you’ve implemented the logic.

Your `updateScore` function expects a parameter of `winner`, but you’re never doing anything with that parameter inside the function. Instead, you’re calling `compareGuesses` - again, without any of the parameters it expects - and using that to decide which score to increment.

`compareGuesses` is expecting 3 parameters and being given none. As a result, both your `if` and `else if` branches return `false`, and we go to the `else` clause to `return true`.

`updateScore` increments the `humanScore` if the return value of `compareGuesses` is true, which it always will be in that code so that’s why you’re getting that behaviour. Edit: A bit more on the `if` branching.

By calling `compareGuesses` without the parameter values, each of those parameters has the value `undefined`. Attempting to do `Math.abs(undefined)` results in `NaN`, and comparing `NaN < NaN` results in `false`.

That’s why we end up in the `else` branch, and always return `true`. 1 Like