Number Guesser code review

Hello!

I’ve struggled through this project a little, and especially been rocky on the bracket placement, so would appreciate any feedback on this snipped of code for the ‘Number Guesser’ game, and why it isn’t working!

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

let compareGuesses = (humanGuess, computerGuess, targetNum) => {
  if (Math.abs(humanGuess --- targetNumber) > (computerGuess --- targetNumber)){
    return true
} else {
  return false
  }
}
const updateScore = () => {
let (humanScore ++) || 
let (computerScore ++);
};
const advanceRound = () => {
let (currentRoundNumber ++);
return currentRoundNumber;
};

Thanks in advance, C :upside_down_face:

Hey digital0384353304!
I may be able to help you out.

Let’s review your code together. First you declare an arrow function and assign it to a constant called generateTarget.

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

In this function you have some mistakes:

  • math.floor() is used with the wrong syntax, it should be math.floor(some-number-goes-here) more info about this here
  • another issue is that you return something that doesn’t exist, targetNumber has not been defined in the global scope, nor in the local scope of the function.

In the next function compareGuess you want to compare the different parameters that are being passed in. What does this line should do?

if (Math.abs(humanGuess --- targetNumber) > (computerGuess --- targetNumber)){

I think you confused yourself here, an equal comparison in JS is made either using == or ===, this question on Codeacademy addresses the difference between the two operators.

The next functions updateScore and advanceRound also have some issues, if you declare a variable with let a variable name must follow. You’re not doing this here. You’re kind of mixing up the variable declaration with let and the logic of a conditional. For example in updateScore:

Review the JS concepts of variables declaration, functions and conditionals in order to correct your code :wink:

Hope this helps :slight_smile:

1 Like

Thank you so much for this response, I knew there were certain concepts that I wasn’t grasping properly, so I will review these again - thank you!

1 Like

Hello everyone. if someone can give me some advice or motivation will be appreciated :slight_smile:
I am confused about this exercise, not much about the instructions and the code I need to write, but more about the usage of visual studio code or even the same exercise in the codecademy website.

I am struggling with the idea of not being able to console.log to test my code in an output screen as I used to.
I am having difficulties transitioning from the previous exercises where I had a screen telling me where my syntax errors are. Now here I am either not looking in the right place, or all I have is a “Make a guess button” that doesn’t work if the code is not right, am I correct?

I am not sure if I should forget about VSC and continue using the Codeacademy website, however, on the Codecademy website I still struggle to work with not having the previous output screen. I suppose this is real coding but I need to get used to this?

Another thing is, In the instructions, it goes from creating a function to have a random number (target) but there is no instruction for a function for the computer to guess the number.? How is the computer going to provide a guess number?
If this is already part of the code created for the exercise this is perhaps the kind of instructions I think are lacking.
Overall what I am trying to say is that this feels like a huge step from where I was to something else.
Your advice is appreciated,
thanks, everyone.

Hi Rene,

You can always open the browser’s console. You may have to filter out a few warnings and error messages which aren’t related to your code, but if you add a console to your code, it will log to the browser’s console.
But in this exercise, you can also open CC’s console:

There are no instructions that you have to do the exercise in your local environment, so you’re not doing it wrong. But of course, you’re free to do it locally. You should just make sure that you copy all files in the directory. This is what the instructions say about it:

Setup Instructions
If you choose to do this project on your computer instead of Codecademy, you can download what you’ll need by clicking the “Download” button below.

This is what the instructions say:

In this project, you’ll write four functions in script.js . We’ve provided some additional JavaScript code in game.js that will call your functions based on user interactions

1 Like

Hi @mirja_t
Your post was very helpful, I wasn’t looking at the output screen below.
I was too obsessed with making sure I include code for everything instead of focusing on the instructions.
Cheers.

Hi again folks, after having reviewed the code to try and get it to function correctly (which I believe I have), I’m left with a few questions.

  1. Where in the code is the string ‘human’ initialised?
  2. What is the need for the Math.abs() object method, that is suggested in the hint?
  3. Does it matter that I have used else statements to assign winner to computer?

Here is my code, without finishing the final requirements of the project so that I can make sure I understand it as thoroughly as possible!

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
let winner;
// Write your code below:
const generateTarget = () => {
  let targetNumber = Math.floor(Math.random() * 10);
  return targetNumber;
};

const compareGuesses = (human, computer, targetNumber) =>{
  
  if (human === computer){
    return true
  } else if ((targetNumber - human) < (targetNumber - computer)){
    return true
  } else {
    return false
  }
}


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

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

Thanks in advance, C :upside_down_face:

Have a look at the file game.js. The value for the human guess is fetched from the input field and then passed as an argument to your function:

const currentHumanGuess = humanGuessInput.value;
// Determine if the human or computer wins:
  const humanIsWinner = compareGuesses(currentHumanGuess, computerGuess, target)

Math.abs() returns a positive number. Think of different scenarios for the values of secret, human guess, and computer guess. What would the returned value be without Math.abs() and how would it affect the result?

Instead of only using ‘if’? Yes, if you use ‘else if’ and ‘else’ the other conditions won’t be checked anymore after one of them was a hit already. That’s more performant and more semantic.

1 Like

Ah thank you so much for this, that all makes perfect sense, after implementing it in my code and making sure that the computerScore was updated properly, I can see why you need Math.abs()!!

Thank you so much :slight_smile:

1 Like