Number Guesser Challenge Project (JavaScript)

Hi,

I can’t get the code to validate the tie condition, could someone tell me why?

I have tried to implement the condition separately as per below.

if(userGuess === computerGuess){
        return true;
    }
   else if(Math.abs(target - computerGuess) > Math.abs(target - userGuess)){
        return true;
    } else{
        return false;
    }

And also have tried to include it in the if condition where I am comparing the guesses:

 if((Math.abs(target - computerGuess) > Math.abs(target - userGuess)) || (userGuess === computerGuess)){
        console.log('usr win');
        return true;
    } else{
        console.log('comp wins');
        return false;
    }

And return true if any of these conditions is fulfilled. But I am getting “Computer Wins” every time there is a tie.

I am logging the values of the guesses and target on the console, as I wanted to check if these are indeed the values selected, and they are, so I can’t really get to the bottom of it.

Any help would be highly appreciated :slight_smile:
Thanks, Flo

Update:

I have implemented the condition with “<=” operator and it works fine.

if(Math.abs(target - userGuess) <= Math.abs(target - computerGuess)){
        return true;
    } else{
        return false;
    }

It would still be good to understand why the first two options did not work.

Thanks, Flo

I kinda followed all steps in the instruction, however the code does not seem to work.

Can anybody help with my code?

Thanks in advance!

The code that calls this function is located in the game.js file. It doesn’t pass an argument to the function, so you shouldn’t be accepting a parameter.

No. It only matters that the function increments currentRoundNumber each time it is called, so there is no reason for an if statement. This function is a perfect candidate for a concise body arrow function, since it only has a single task:

const advanceRound = () => currentRoundNumber++;

You very much over complicated this project. Stick to what the steps tell you to do. Also you should be able to accomplish the compareGuesses function with only one if statement

Hello, my name is Christine and this is day 12 of my Javascript adventure. I have been teaching music for 30 years but I’ve always wanted to learn to code and I finally have the time! I love it and plan to continue.
Here is my completed “Number Guesser” project. It works!

2 posts were split to a new topic: Number guesser challenge project

I made some improvements to the project: I made it impossible to type manually the number and to go beyond the number 9 with the button.


Also on my GitHub : https://github.com/Dyrits/WEBDEV-CC/tree/master/Challenge%20Projects/Number%20Guesser

1 Like

Good job, you’re learning fast. I left a comment on your project.

1 Like

Hi, I am unable to make the alert() function work.

I have tried implementing a condition to check if guess>9 in compareGuesses but it did not work.

I went to game.js and added the window.alert(‘input out of range’); in the handleValueChange, as per screen shot below, but still not working.

Any advice would be appreciated.
Thanks, Flo

Is it possible to check for different conditions in one if statement? My mind is blowing to include all these combinations in one statement:

  1. Target > userGuess && Target > compGuess //Let’s say target: 9, user: 6, comp: 5
  2. Target < userGuess && Target < compGuess //Let’s say target: 4, user: 6, comp: 5
  3. Target < userGuess && Target > compGuess //Let’s say target: 4, user: 6, comp: 3
    4 Target > userGuess && Target < compGuess //Let’s say target: 7, user: 6, comp: 9

Yes but you don’t need to for this project. Make sure you read the project instructions carefully

Hi everyone,
It’s my first time on the forum, thank you in advance for your help. I’m struggling with this exercice.

Nothing happens at any level when I click on the "Make a guess"and I don’t understand why and what I missed with my function compareGuesses() (line 11).

Here is my code
https://gist.github.com/8422c5cc9506f4177a0a10c9fbeb09cc

Thank you

When writing an if else statement, is there supposed to be a condition for the else?
Why are you declaring currentRoundNumber twice? It is already declared as a global variable at the top of your code.

It’s my mistake I didn’t realized that currentRoundNumber was on top.
For the others stuff I effectively forgot an if line 16 and also an error of syntax line 25.
It seems to work right now.

Just a quick question I’m not sure to understand the logic the compareGuesses() of the correction. The return is necessarily considered as true, that why we don’t need create other conditions with >= and === ?

const compareGuesses = (humanGuess, computerGuess, targetGuess) => {
  const humanDifference = Math.abs(targetGuess - humanGuess)
  const computerDifference = Math.abs(targetGuess - computerGuess)
  return humanDifference <= computerDifference;

Think of it like a math expression. You’d evaluate it fully, until it’s a single value.

return (
  Math.abs(targetGuess - humanGuess) <= Math.abs(targetGuess - computerGuess)
)

Insert different values for targetGuess, humanGuess, computerGuess. What’s the outcome of that expression?

It’s not always true, if it was, you could write this instead:

const compareGuesses = () => true

…which would be pretty useless, because then the human always wins.

1 Like

Can anyone help with this ? Followed all steps, it’s not working

Seems like you forgot to add a { } in your code. You also forgot to add “=>” in your advanceRound function.

Blockquote
if(Math.abs(targetGuess-humanGuess)<= Math.abs(targetGuess-compGuess)) {
return true;
} else {
return false;
}

For your updateScore function, “winner” is a parameter that is being inputted into the function when we call it. Our code will either input ‘human’ or ‘computer’ when calling the updateScore function depending on who wins. Our compareGuesses function returns either ‘true’ or ‘false’. We did not have to actually code the part that says the winner is ‘human’ or ‘computer’. That part is probably in the coded section that CA already provided. We aren’t trying to declare the who the winner is in our updateScore function like you were trying to do. We are merely adding a point to the winner that has been declared for us in a hidden part (CA’s provided code) that works in conjuction with our compareGuesses function.

So, when the code actually calls for the updateScore function, it will look something like

Blockquote
updateScore(‘human’); or updateScore(‘computer’);

FYI, we didn’t actually have to call in the updateScore function for this project, so we didn’t have to actually write the code like above.

With that in mind, our updateScore function will be read like,

if winner === ‘human’, then humanScore += 1, else if winner === ‘computer’, then computerScore +=1

I think manipulating game.js is beyond the scope of this project.

I had the code

Blockquote
function compareGuesses(human, computer, target) {
if (human > 9 || human < 0) {
alert(“invalid input”);
} else if (getAbsoluteDistance(human, target) <= getAbsoluteDistance(computer,target)) {
return true;
} else {
return false;
}
}

1 Like

Thank you all for your help

Here’s my solution:

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, 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;
 }
}
  
  const updateScore = winner => {
    if (winner ==='human') {
      humanScore++;
    } else if (winner === 'computer') {
      computerScore++
    }
  };
  
 
  const advanceRound = () => currentRoundNumber++;