Number Guesser Challenge Project (JavaScript)

My solution:

function generateTarget() {
return Math.floor(Math.random() * 10);
}

function compareGuesses(userGuess, compGuess, targetNum) {
return Math.abs(targetNum - userGuess) <= Math.abs(targetNum - compGuess)
? true
: false;
}

function updateScore(winner) {
winner === “human” ? humanScore++ : computerScore++;
}

function advanceRound() {
currentRoundNumber++;
}

Hi @g2431783256, thanks for sharing your code :smiley:. Check your generateTarget function. Currently it is returning 0 - 8 but I believe it should be returning 0 - 9. Also, in your compareGuesses function, remember to check the case where human and computer tie. Cheers!

Nice work @mjwakex. I see you wrote your own absolute distance logic! I think it would help the readability if you extracted that logic to its own getAbsoluteDistance function and called that from within compareGuess. Also, in your generateTarget function, you could try returning the random number immediately instead of assigning it to a variable first. Looking forward to seeing more of your work. :smiley:

https://github.com/darwin-09/codecademy-number-guesser

Here’s my solution. I have finished both extra exercises.

The problem I had encountered was no windows showing up when input value is smaller than zero or larger than nine.

I finally figured out that the function alert() I defined might be confused by the build-in syntax, alert(), of JavaScript.

In JavaScript, we can use window.alert() to have an alert box popped up, but this window.alert() can also be written as alert(), which is identical to the function name I defined.

After realized this difference, I changed the name of my function, and every thing works.


Here is my solution for this project.

I feel like I’m missing something because when I try to play the game on my browser, nothing happens. I enter my number but it doesn’t show a target number or computer guess. I’m not sure what’s going wrong or if it’s just my browser?

Here is my solution here.
About Step 4 is quite interesting. Despite the seeing multiple people able to refactor it to a ternary statement, and the logic make sense too, however, if I try with my code, the computer became the ultimate winner. Does any one have some thought about that?

Thanks for the tip. That will make it more economical. Cheers

This is my solution.

Its because the compareGuesses() is not doing anything.
if(human === computer){
return true; her you are returning true to nowhere(no out put because these are not being compare to the ‘closest target’)

else if(human === target || Math.abs(human - target) < Math.abs(computer - target){
return true;

here the way you are trying to get the abs value is not correct. the statement after || is to get the closest value yet its not being compared to either human or computer’s guess. its not returning a value too.

here is how I tried to do it. there might be a better way of doing it but this works.

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

  const closest = (Math.abs(computer - target) < Math.abs(human - target) ? computer : human);

  if(human === closest){

    return true;

  } else if(computer === closest){

    return false;

  } else if(human == closest && computer === closest) {

    return true;

  }

};

and also don’t forget to return the value of your advanceRound() at the end

Hi, its my code

General question:

I’m having trouble understanding how the generateTarget() function links to a value for the target parameter in the compareGuesses() function. It seems in the solution there is no relationship between generateTarget() and compareGuesses(), but the code runs. How is this?

Thanks in advance for your help.

Here is my solution :

Hello everyone!
Here is my solution:

Hi !
I need help, can’t move to next round,

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

// Write your code below:
  const generateTarget = () => {
  let target = Math.floor(Math.random () * 10);
  return target;
  
};


const compareGusses = (humanGuess, computerGuess, turgetNumber) => {
  

   if (Math.abc(turgetNumber - humanGuess) <= Math.abc(turgetNumber - computerGuess)) {
    return true;}
      else {
      return false;
    }
};

const updateScore = winner => {

  if(winner === 'human') {

    humanScore += 1;
  }
  else if(winner === 'computer') {
    computerScore += 1;
  }
  
  
  };
  
  const advanceRound = () => {

    currentRoundNumber +=1;
  };

  const getAbsoluteDistance = (number1, number2) => {

    return Math.abc(number1 - number2);
  
  

    };














Hello, i am a complete beginner and I have a lot of confusions on how to run codes on VS Code.
I’ve the basic code on point for this project but I’m not able to run it in my browser.

Here is my code for the number guesser game:

It works okay but I have one detail I think I missed. Because when I try to input a number larger than nine, I was able to code an alert to show the player to pick number from 0-9 only, but the computer would still automatically win and get a point. I wonder if there is something I can do in the code to prevent this behavior? Thanks!