Number Guesser Challenge Project (JavaScript)

This was my solution. I think the compareGuesses() function is actually easier this way than in the solution, but I’d like to know what you think about it, maybe it works just by a stroke of luck!

Hello, @lpianta2321219555, and welcome to the forums. Your compareGuesses() function is totally fine, but you could make it more concise if you wanted to. Consider the following example, and then think about how you could apply the same technique to your function.

const returnTrueOrFalse = (value1, value2) => value1 >= value2; //what is returned?

//what will be printed to the console?
console.log(returnTrueOrFalse(5, 4)); 
console.log(returnTrueOrFalse(4, 5)); 

The solution code is so much prettier than mine :laughing:

A couple of questions:

Could someone explain why this line of code is giving us the result we want?
return humanDifference <= computerDifference;

Also,
if (winner === ‘human’)
and
else if (winner === ‘computer’)…I don’t understand how the computer would know what ‘human’ or ‘computer’ is since it wasn’t referenced before in the code…

Sorry if these are dumb questions :confused:

Can you somebody help me find out what’s wrong in the logic on the copareGuess function ? Thank you.

https://github.com/coffeesp00ns/teaspoon-data/blob/master/numberguesserscript.js

This is what I did. It’s just the basics, no additional challenges.

The computer keeps winning even if the userDifference is less than or equal to computerDifference.
Here is my current code:

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

// Write your code below:
//functions
const generateTarget = () =>  Math.floor(Math.random() * 10);


const compareGuesses = (userGuess, computerGuess, target) => {
  const userDifference = () => Math.abs(userGuess - target);
  const computerDifference = () => Math.abs(computerGuess - target);

  if(userDifference <= computerDifference) { 
    return true;
  } else {
  return false;
  };
};

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

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

‘human’ and ‘winner’ are defined in the game.js file

This is my solution. I added in an else if (winner === ‘computer’)

Hi everyone. Check out my solution to the number guesser game. You can access it via the link to my GitHub repo shown: https://github.com/Generalsoi/number_guesser

Quick question for you, since we are probably learning the same stuff here, and in a similar order (I would assume). How did you know that alert(), as it is written in the extra challenge for this project, is actually supposed to be window.alert()?
This hasn’t been mentioned in any of the previous lessons, and I erroneously kept trying to write “function alert() {…}” and it wasn’t working.

Hello everyone, I have a quick question, since we are all probably learning the same stuff here, and in a similar order (I would assume). How did some people know that alert(), as it is written in the extra challenge for this project, is actually supposed to be window.alert()?
This hasn’t been mentioned in any of the previous lessons, and I erroneously kept trying to write “function alert() {…}” since the extra challenge in this project just mentions “Add functionality to check whether the user guess is between 0 and 9 and alert() the user that their number is out of range”. Thanks to anyone for the reply!

Hey, I’m not sure if you found the solution but what I did was return true or false depending if the value was valid, and in the game.js file verified the input with the function in an if statement. If it returns true, it means the value was valid and the rest of the code runs if it’s not valid it waits for a valid input.

if(inputAlert(currentHumanGuess) {
everything between the if statement and the first }); (not including that line)
}

right after the if you should have }); and then the “nextRoundButton.addEventListener(‘click’, () => {” line

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