Number guesser project

Hello everyone I am doing the number guesser project and I am not understanding why my code won’t work properly. I have looked on the forums and have found answers but I am determined to make it my own. Would anyone have any advice?

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

const generateTarget = () => {

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

};

const compareGuesses = (humanScore, computerScore, numberGuess) => {

if (humanScore === numberGuess ) {

  return 'Human Wins!';

}else if

  (computerScore === numberGuess ) {

      return "Computer Wins!"

  } else {

      return 'It is a tie!'

}

}

const updateScore = (humanScore) => {

if(humanScore === winner) {

    human++;

}else {

    computer ++

};

};

function advancedRound() {

currentRoundNumber++;

}

compareGuesses();

Hi,

We can help you out, can you format your code when you have a moment, as it’ll speed up the process :slight_smile: : [How to] Format code in posts

In terms of the code, consider reviewing the instructions once more.
Currently you are comparing the score of the player/computer with the numberGuess, but score is for when you/computer gets a guess correct!

I think it helps sometimes to just try to draw out a “map” of what the whole set of instructions wants you to do so you have a clearer idea of your end goal. No need to be technical in terms of what the code needed is first.

Sure i can format it for you:

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

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

 const compareGuesses = (humanScore, computerScore, numberGuess) => {
  if (humanScore === numberGuess ) {
      return 'Human Wins!';
  }else if 
      (computerScore === numberGuess ) {
          return "Computer Wins!"
      } else {
          return 'It is a tie!'
  }
 }

 const updateScore = (humanScore) => {
    if(humanScore === winner) {
        human++;
    }else {
        computer ++
    };
 };

  function advancedRound() {
    currentRoundNumber++;
}

  compareGuesses();


compareGuess takes three arguments. When you call it, you call it with none:


There doesn’t seem to be anything else wrong with it at first glance. Have you tried printing out the value returned from compareGuess?

If I am following you correctly, this is what you are trying to tell me to do:

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

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

 const compareGuesses = (humanScore, computerScore, numberGuess) => {
  if (humanScore === numberGuess ) {
      return 'Human Wins!';
  }else if 
      (computerScore === numberGuess ) {
          return "Computer Wins!"
      } else {
          return 'It is a tie!'
  }
 }

 const updateScore = (humanScore) => {
    if(humanScore === winner) {
        human++;
    }else {
        computer ++
    };
 };

  function advancedRound() {
    currentRoundNumber++;
}

   return compareGuesses(humanScore, computerScore, numberGuess);

if so that didn’t really have an effect on anything.

Hi,

I think the rules of this game need some clarification:

  • score refers to point total. Whoever wins a round gets more points to their score.
  • guesses are what you fight with. A computer takes a guess, you take a guess (although in this program it’s auto generated) and those guesses are compared against a target.
  • whoever is closest in their guess to the target gets their scores updated.

compareGuesses() has three parameters representing the user (human) guess, a computer guess, and the secret target number to be guessed."

Right now your compareGuesses() function is taking the parameters of scores, not human/computer guess. So it’s not quite what’s asked for in the directions.

2 Likes

If I might also add some insight to your problem. The function, compareGuesses, should compare how far off each of the guesses (userGuess and computerGuess) are from the target number. In your current function, you are only comparing the guesses to see if they equal to the target number or not. The key to the game is to get as close to the target number as possible. Whoever is the closest wins, or if both the computer and user guess the same number it results in the user winning.
I hope this helped!

Thank you everyone! after doing a lot of research and pulling out my hair I came up with this solution:

let computerScore = 0;
let currentRoundNumber = 1;

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

 const compareGuesses = (human, computer, target) =>{
     const humanGuess = Math.abs(human - target);
     const computerGuess = Math.abs(computer - target);
     if (humanGuess <= computerGuess) {
         return true;
     } else {
         return false;
     }
 }

 const updateScore = string => {
     if(string === 'human') {
         humanScore += 1;
     }else if (string === 'computer') {
         computerScore += 1;
     }
 }


 const advanceRound = () => {
     currentRoundNumber += 1;
 }


This is what worked for me!

That’s good!

It’s definitely getting there. Now that the logic is layed out it just needs to be put to action together!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.