Number guesser

Hey Stetim,

Hopefully you can guide me in the right direction here. I have an issue with my code showing error “unexpected identifier” when I run it. I’ve cross-referenced my code with Codecademy’s solution but I can’t seem to figure out where the impasse is. Here’s my code below.

Edit: Let me add as well that I don’t quite understand these variables in the official solution. Do you need to define new variables or can’t you just use if/else statements as I’ve done in my solution? Just wondering if there’s a practical reason to favour one over the other.

const humanDifference = getAbsoluteDistance (argument1, argument2)
const computerDifference= getAbsoluteDistance (argument1, argument2)

Please help!

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

// Write your code below:


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

}

const compareGuesses = (humanGuess, computerGuess, secretTargetNumber) => {
  if Math.abs((secretTargetNumber - humanGuess) > Math.abs(secretTargetNumber - computerGuess)){
    return false
    } else if Math.abs((secretTargetNumber - humanGuess) === Math.abs(secretTargetNumber - computerGuess)) {
      return true
      } else if Math.abs((secretTargetNumber - humanGuess) < Math.abs(secretTargetNumber - computerGuess)) {
          return true
  }else{
      return "Error!"
  }
  
    
}
        
const updateScore = winner => {
    
    if (winner = "human") {
        humanScore++;
    
    } else if (winner = "computer") {
            computerScore++;
        
        }else{
            return "Error";
            }
}

const advanceRound = () => {
    currentRoundNumber = currountRoundNumber++;



}


without knowing the exercise (please include exercise url), i can’t see the solution.

I would argue (based in what i saw in the other topic) that the exercise solutions is easier to read, maintain and test.

your function does a lot at once.

also, conditions require the use of parentheses:

if (condition){}

the parentheses are just missing in your case

2 Likes

Woops! here’s the exercise.

https://www.codecademy.com/practice/projects/number-guesser-independent-practice

1 Like

In that case, i stand corrected, i would however do this:

const humanDifference = Math.abs(targetGuess - humanGuess)
const computerDifference = Math.abs(targetGuess - computerGuess)

and then use these variables for your comparison, makes it a lot more readable. And it avoids duplicate code.

also, it seems to be a simple if/else, if the distance to the targetGuess is the same, the user wins.

4 Likes

Ahhhh, I missed that… lol. Thought I could get away with starting after Math.abs()!

And yeah, the solution’s code is waaay easier to read, for sure. Really appreciate your quick help!

1 Like

I did it a little differently but your variables here helped me understand. Thanks!