Guessing Game

Here is my code, but the computer wins all the time, even when it is not supposed to.

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

// Write your code below:

let generateTarget = () => {
  return Math.floor(Math.random() * 9);
}

let compareGuesses = (humanGeuss) => {
  let humanGuess = '';
  let computerGuess = Math.floor(Math.random() * 9);
  if (Math.abs(generateTarget - humanGuess ) < Math.abs(generateTarget - computerGuess )) {
  return 'true'; 
  } else if (Math.abs(generateTarget - humanGuess) > Math.abs(generateTarget - computerGuess )) {
    return 'false';
  }
}
let updateScore = (winner) => {
   if (winner === 'human') {
     humanscore++;
   } else if (winner === 'computer') {
     computerScore++;
   }
 }
let advanceRound = () => {
  currentRoundNumber += 1
}

Here is my code, but the computer keeps winning even when it is not supposed to.

We should not be resetting the parameter.

I tried to remove it, but it made worse. The game will not declare the winner anymore nor allow me to move to the next round.

Actually it stays the same. The computer is still winning.

I notice that the parameter here is misspelled

Is that done on purpose?

What about this line? Is the typo the problem?

1 Like

Your compare guesses function should not be assigning values to either humanGuess or computerGuess. It should also not be calling the generateTarget function. Yours doesn’t call generateTarget, but refers to it as though perhaps you intended to. Review the instruction:
image

The call to compareGuesses from game.js looks like this: compareGuesses(currentHumanGuess, computerGuess, target). The values are supplied as arguments. You need 3 parameters to accept the arguments passed.

For reference here’s a link to the project: https://www.codecademy.com/practice/projects/number-guesser-independent-practice

I’m afraid I don’t follow you.

Currently, your compareGuesses function as written returns undefined which is falsy, so the computer always wins. We can see proof of this by adding a few console.log() statements:

let generateTarget = () => {
  return Math.floor(Math.random() * 9);
}

let compareGuesses = (humanGeuss) => {
  let humanGuess = '';
  let computerGuess = Math.floor(Math.random() * 9);
  if (Math.abs(generateTarget - humanGuess ) < Math.abs(generateTarget - computerGuess )) {
  return 'true'; 
  } else if (Math.abs(generateTarget - humanGuess) > Math.abs(generateTarget - computerGuess )) {
    return 'false';
  }
  console.log('generateTarget: ' + generateTarget)
  console.log('humanGuess: ' + humanGuess)
  console.log('computerGuess: ' + computerGuess)
  console.log('Result of Math.abs(generateTarget - humanGuess): ' + Math.abs(generateTarget - humanGuess))
}

console.log('The return value of compareGuesses is: ' + compareGuesses(1, 3, 1))

Output:

generateTarget: () => {
return Math.floor(Math.random() * 9);
}
humanGuess:
computerGuess: 4
Result of Math.abs(generateTarget - humanGuess): NaN
The return value of compareGuesses is: undefined
undefined has a bool value of: false

The function should look similar to this:

const compareGuesses = (humanGuess, computerGuess, target) => {
  //code to determine which guess is closer to the target
  //use the parameters in this function
  //don't make new calls to any other functions unless/until you try to complete step 7,
  //and create a new getAbsoluteDistance() function
  //return true if the humanGuess is closer to Target or false if the computerGuess is closer
}

I tried it, but it did not work.

Could you post your code that you tried, please?

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

// Write your code below:

let generateTarget = () => {
  return Math.floor(Math.random() * 9);
}

const compareGuesses = (humanGuess, computerGuess, target) => {
  
  if (Math.abs(target - humanGuess ) < Math.abs(target - computerGuess )) {
  return 'true'; 
  } else if (Math.abs(target - humanGuess) > Math.abs(target - computerGuess )) {
    return 'false';
  } else {
    return 'true';
  }
}
let updateScore = (winner) => {
   if (winner === 'human') {
     humanscore++;
   } else if (winner === 'computer') {
     computerScore++;
   }
 }
let advanceRound = () => {
  currentRoundNumber += 1
}

Code formatted by Moderator edit.

We need to return the boolean value true rather than the string literal 'true'. Same goes for false.
Also, please review this post: How do I format code in my posts? Formatting your code when you post it will make it much easier for others to help you. :slightly_smiling_face:

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

// Write your code below:

let generateTarget = () => {
  return Math.floor(Math.random() * 9);
}

const compareGuesses = (humanGuess, computerGuess, target) => {
  
  if (Math.abs(target - humanGuess ) < Math.abs(target - computerGuess )) {
  return true; 
  } else if (Math.abs(target - humanGuess) > Math.abs(target - computerGuess )) {
    return false;
  } else {
   return true;
  }
}
let updateScore = (winner) => {
   if (winner === 'human') {
     humanscore++;
   } else if (winner === 'computer') {
     computerScore++;
   }
 }
let advanceRound = () => {
  currentRoundNumber += 1
}

I did as you said, but now it won’t tell when human wins. Also it does not allow me to go to the next round.
thanks for your help.

One last very small detail. Compare the following:

If you were to run your code with a browser console window open, you’d see this error:
ReferenceError: humanscore is not defined

This error prevents the remaining code from executing, so you weren’t seeing what is supposed to happen when the human wins. Correct this last error, and you should be all set. :slightly_smiling_face:

It worked.
Thank very much.

1 Like

You are very welcome!