# Number Guesser Project focusing on functions

Hello, I am having trouble seeing why my code is not working the way it’s suppose to. I’m not getting any errors, it’s probably the logic of what I wrote but I’m not getting any clarity. Any insights/hints would be appreciated. Thank you.

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

// Generate random target number from 0 to 9
const generateTarget = () => {
randomTarget = Math.floor(Math.random()*10);
return randomTarget;
}
// Compare user and computer guess with target number
const compareGuesses = (user, computer, secret) => {
secret = generateTarget();
if (user === secret && computer === secret) {
return true; // return true if human wins
}
if (Math.abs(user-secret) > Math.abs(computer-secret) ){
return false; // return false if computer wins
}
else {
return true;
}
}
// Increases the winner's score after each round
const updateScore = (winner) => {
if (winner = 'user') {
humanScore++
}
else if (winner = 'computer'){
computerScore++;
}
}

if (humanscore++ || computerScore++){
return currentRound++;
}
}
``````

when i run your code, i do get an error:

looking at the function declaration:

``````const advancedRound(){
``````

and comparing it with a valid one:

``````const generateTarget = () => {
``````

i can see the problem.

also, here:

``````if (winner = 'user') {
``````

you use the assign operator, not a comparison operator. Thus this condition will always be true.

3 Likes

Thank you. For some reason, the errors weren’t running on my page.

1 Like

I’m still lost on the random number generator. The computer is winning when the user should have won and vice versa. What can I do?

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

const compareGuesses = (user, computer, target) => {
target = generateTarget();
let userDistance = Math.abs(user - target);
let computerDistance = Math.abs(computer - target);

if (user === target && computer == target) {
return true;
}
else if (userDistance < computerDistance ){
return true;
}
else {
return false;
}
}
``````
1 Like

difficult to say without seeing the full code

1 Like

doesn’t produce expected result -> look at the steps carried out -> change the incorrect step

1 Like