Number Guesser Challenge Project (JavaScript)

here is my go at the project including the two extended tasks!

Hello ladies and gentlemen. Would you mind explaining me what am I doing wrong that if computer guess is the same as my guess, computer is getting the points, although I programmed it to give human points when tied? Code:

const compareGuesses = (humanGuess, computerGuess, actualNumber) => {

if (humanGuess === computerGuess) {

 return true;

} else if (Math.abs(humanGuess - actualNumber) < (Math.abs(computerGuess - actualNumber))) {

  return true;

} else {

  return false;

}

}

never mind, I figured out what’s wrong. Here’s the solution for anyone having same issue:

In my function, compareGuesses, all I needed is one “if” and one “else”, no “if else”. I remove the “if (humanGuess === computerGuess)” conditional statement, and keep the second conditional statement in place, but instead of “<” operator, I make it into “<=”.

Thank you for coming to my TED talk. Goodbye

here is what i came up with for any one having trouble;
/--------------------------------------------------------------------------/
const generateTarget = ()=>{
return Math.floor(Math.random()*10);
};

const compareGuesses = (human, computer, target)=>{

human = Math.abs(target-human);
computer = Math.abs(target-computer);

if(human <= computer)
    return true;
else
    return false;

}

const updateScore = (winner)=>{

if(winner === 'human')
    humanScore++;
if(winner === 'computer')
    computerScore++;

}

const advanceRound = ()=>{
currentRoundNumber++;
}
/--------------------------------------------------------------------/

2 posts were split to a new topic: Are Javascript built-in objects like Math always available?

Hey guys - I tried the project and most of my code seems to be fine. However, whatever I do, the computer wins… even if I guess the number. I’m stuck and I can’t figure it out. I realize there’s a simpler solution (had a look at the solution offered by Codecademy), but I also want to know why my code doesn’t work.

The issue (I guess) is somewhere from in between line 12 to 46…

const compareGuesses = (humanGuess, computerGuess, targetGuess) => {

  const humanTarget = (humanGuess, targetGuess) => { //calculates the numeric distance to targetGuess for human.

    if (humanGuess > targetGuess) {
      humanTarget = humanGuess - targetGuess;
    }
    else {
      humanTarget = targetGuess - humanGuess;
    }
    }
  const computerTarget = (computerGuess, targetGuess) => { //calculates the numeric distance to targetGuess for computeren.

    if (computerGuess > targetGuess) {
      computerTarget = computerGuess - targetGuess;
    }
    else {
      computerTarget = targetGuess - computerGuess;
    }
  }
  
if (humanGuess === computerGuess) { // if human and computer is tie, point to human

return true;
}

else if (humanTarget < computerTarget) { //if human has the shortest distance to targetGuess 

  return true;
}

else { //if the computer has the shortest distance to targetGuess
  return false;
}
}

Hello, @betaace48850, and welcome to the forums.

This code has some problems:

const compareGuesses = (humanGuess, computerGuess, targetGuess) => {

  const humanTarget = (humanGuess, targetGuess) => { //beregner den numeriske afstand til "targetnumber" for human.

    if (humanGuess > targetGuess) {
      humanTarget = humanGuess - targetGuess;
    }
    else {
      humanTarget = targetGuess - humanGuess;
    }
    }
  const computerTarget = (computerGuess, targetGuess) => { //bergner den numeriske afstand til "targetnumber" for computeren.

    if (computerGuess > targetGuess) {
      computerTarget = computerGuess - targetGuess;
    }
    else {
      computerTarget = targetGuess - computerGuess;
    }
  }
  
if (humanGuess === computerGuess) { // hvis human og computer gætter ens er der tie = point til human.

return true;
}

else if (humanTarget < computerTarget) { //hvis human har mindst afstand til "targetnumber" vinder human.

  return true;
}

else { //hvis computeren har mindst afstand til target vinder computeren.
  return false;
}
}

Do you want humanTarget and computerTarget to be variables or functions? You’re kinda trying to use them as both. If they are variables, they need a value assigned to them. If they are functions, and you want to use the value generated inside the function later, you’ll need to return that value.

The functions, humanTarget and computerTarget are also never called. In this line, for example, else if (humanTarget < computerTarget) you are comparing your two functions rather than the values calculated and then lost by the functions.

If either function were called, you would get an error for trying to re-assign a value to a const variable here:

    if (humanGuess > targetGuess) {
      humanTarget = humanGuess - targetGuess; //humanTarget is the same name as the encompassing function which you declared with const using arrow function syntax
    }
    else {
      humanTarget = targetGuess - humanGuess; //same thing here
    }
1 Like

Hi all,

I’ve completed my code and think it’s ok. Have also compared it to a few other people’s solutions and seems pretty good but I’m not sure how to actually link them all up to play?

Am I being thick? Surely I need to write another function to generate the computer’s guess and link all my functions together. What am I missing here?

Thanks in advance

Hello, @tdcshep0050395884, and welcome to the forums.

If you click on the folder icon in the top left corner of the code editor, you’ll see a file called game.js. Open it, and you’ll see the code that calls each of the functions that you wrote at the appropriate times. :slightly_smiling_face:

Hi @midlindner! Thanks for getting back to me!

I have now tried to run the game.js page as you suggested and get the following ReferenceError.

Surely this page should not be throwing up errors though as it is provided by CodeAcademy?

Any ideas what I should do?

Thanks in advance!

1 Like

That’s just an issue with how the learning environment works. When you click Run with game.js open in the code editor, the insertion point of the entire program is off, so it won’t work properly. I only mentioned looking at game.js, so you could see the code that calls the functions that you wrote.

1 Like

Dropping by to leave my solution here :slight_smile:

Thank you so much! This is what I needed to finish the project :slight_smile:

Thanks!

1 Like

Dropping my solution here

I’d love to see work from anyone if they were able to finish task no. 8 (getAbsoluteDistance() & alert()).
Here is what I came up with:

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

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

const compareGuesses = (humanGuess, computerGuess, targetNumber) => {
  let humanGap = Math.abs(humanGuess - targetNumber);
  let computerGap = Math.abs(computerGuess - targetNumber);

  if (humanGap <= computerGap) {
    return 'human'
  }
}

const updateScore = winner => {
  'human' ? humanScore ++ : computerScore ++
  }

function advanceRound() {
  return currentRoundNumber ++
}

Reading through some post I already noticed people are using Math.abs() which I didn’t know about yet :slight_smile: Anyhow here is my solution:

Hi all, I was struggling for a while but I did it somehow. I was trying to do the extra work and had to search a bit but I hope this code does what it is supposed to do. But still, I would like to stop the code from working further in case the alert is displayed to a user. Even if you put the wrong number, let’s say 30, the error is displayed but in that case the game goes on and the computer is the winner. Could anybody help me with this?
Thanks a lot.

I am wondering how compareGuesses on line 9 of the solution code knows that the targetGuess parameter is the random number generated with the generateTarget function?