JS: Challenge Project: Number Guesser

Hello all,

I am not getting the answer that I suppose to get from the function ‘compareGuesses’.
It always return true which is user always wins.

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below:

const generateTarget=()=>{

return Math.floor(Math.random()*10);

};

const compareGuesses=(targetNumber,userGuess,compGuess)=>{

let userDifference = Math.abs(generateTarget()-userGuess);

let compDifference = Math.abs(generateTarget()-compGuess);

if ( userDifference < compDifference ) {

return true;}

else if (userDifference > compDifference) {

return false;}

else {return true;}

};

const updateScore=()=>{

if (‘human’) {

humanScore=humanScore+1;}

else if (‘computer’) {computerScore=computerScore+1;}

};

const advanceRound=()=>{

currentRoundNumber=currentRoundNumber+1;

};

Hi, nnbek
I made a few changes in you code with comments.
Now it’s working (at least on my computer :slight_smile: )
Please have a look.
This is my first debugging experience. Sorry if I did something wrong at advance :wink:

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below:

const generateTarget=()=>{

return Math.floor(Math.random()*10);

};

// Here I put parameters in order according Codecademy task
const compareGuesses=(userGuess, compGuess, targetNumber)=>{

// We need to convert data type for variables "userGuess" and "compGuess" to number
userGuess = parseInt(userGuess);
compGuess = parseInt(compGuess);

// I think "targetNumber" was already generated outside this function and passed as a parameter
// Here we use this parameter
let userDifference = Math.abs(targetNumber-userGuess);

let compDifference = Math.abs(targetNumber-compGuess);

if ( userDifference < compDifference ) {

return true;}

else if (userDifference > compDifference) {

return false;}

else {return true;}

};

// We need to pass "winnerName" into this function and use it at conditional statements
const updateScore=(winnerName)=>{

if (winnerName === 'human') {

humanScore=humanScore+1;}
else if (winnerName === 'computer') {computerScore=computerScore+1;}

};

const advanceRound=()=>{

currentRoundNumber=currentRoundNumber+1;

};

Hello, Romaussie.

Thank you very much for your help! :slightly_smiling_face:

It works very well. :+1:

I highly appreciate it.

1 Like