Number guesser challenge project

Hi everyone, my name is Jeanine and this is my second time giving this project a go. I tried editing my compareGuesses f(x) as I thought that might be the issue…however the code is still not working and I’ve even pasted it into Visual Studio Code to see if it might be a syntax error. Here’s my code below, any feedback would be much appreciated - thanks!

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

// Write your code below:

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


//this function will be called each round to determine which guess is closest to the target number and determine a winner 
const compareGuesses = (humanGuess, computerGuess, targetGuess) => {
 var humanDifference = Math.abs(targetGuess - humanGuess);
 var computerDifference = Math.abs(targetGuess - computerGuess);
 if (humanDifference < computerDifference) {
   return "You won!";
 } else if (humanDifference > computerDifference) {
   return "The computer won";
 } else {
   return "You won!";
 }
}

//this function will be used to correctly increase the winner's score after each round
const updateScore = winner => {
  if (winner ==='human') {
    humanScore++;
  } else if (winner === 'computer') {
    computerScore++
  }
};

//this function will be used to update the round number after each round
const advanceRound = () => currentRoundNumber++;

There is a syntax error, did visual studio code highlight it?

1 Like

Yes sorry, I’ve fixed that now to:

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

The output page is now letting me play the game, but there must still be a problem as the human guess is now the winner no matter what

Update: I’ve fixed the code and got it to work now; I had to enter a value of true or false with my compareGuesses function:

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

// Write your code below:

const generateTarget = () => {
    return Math.floor(Math.random() * (10))
}
  
  
  //this function will be called each round to determine which guess is closest to the target number and determine a winner 
const compareGuesses = (humanGuess, computerGuess, targetGuess) => {
 var humanDifference = Math.abs(targetGuess - humanGuess);
 var computerDifference = Math.abs(targetGuess - computerGuess);
 if (humanDifference < computerDifference) {
   return true;
 } else if (humanDifference > computerDifference) {
   return false;
 } else {
   return true;
 }
}
  
  //this function will be used to correctly increase the winner's score after each round
  const updateScore = winner => {
    if (winner ==='human') {
      humanScore++;
    } else if (winner === 'computer') {
      computerScore++
    }
  };
  
  //this function will be used to update the round number after each round
  const advanceRound = () => currentRoundNumber++;
  

But any feedback is most welcome :slight_smile:

To check my understanding further, I’m having a try at the first question to extend my project further:

  • You probably calculated the distance from the computer guess to the target and from the human guess to the target. Move this into a separate getAbsoluteDistance() function that takes two numbers and returns the distance, and then use that inside your compareGuesses() function.

This is what I’ve updated the code to but while the output allows me to guess a number, it doesn’t determine a winner:

function getAbsoluteDistance (targetGuess, humanGuess, computerGuess) {
 return  humanDifference = Math.abs(targetGuess - humanGuess);
 return  computerDifference = Math.abs(targetGuess - computerGuess);
}

  
const compareGuesses = () => {
  getAbsoluteDistance();
 if (humanDifference < computerDifference) {
   return true;
 } else if (humanDifference > computerDifference) {
   return false;
 } else {
   return true;
 }
}