Number Guesser project

Hello,

I’ve been browsing this forum a bit and hope to finaly input working code, but that’s not the case. It looks like that:

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

// Write your code below:

generateTarget = () => {
  let randomNumber = math.floor(math.random()) *10;
  return randomNumber;
} 

compareGuesses = (humanGuess, computerGuess, secretNumber) => {
  if(humanGuess === computerGuess) 
    { return true;
  } else if (Math.abs(secretNumber - humanGuess) > Math.abs(secretNumber - computerGuess)) 
    { return false; 
  } else if (Math.abs(secretNumber - humanGuess) < Math.abs(secretNumber - computerGuess)) 
    { return true;
  }
} 

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

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

VSC, where I wrote it, did not return any mistakes. Other that that I have a question for more experienced users:
I see people using various ways to write down a function:

* const compareGuesses = (humanGuess, computerGuess, targetGuess) => {}
* compareGuesses = (humanGuess, computerGuess, targetGuess) => {} 
* function compareGuesses(humanGuess, computerGuess, targetGuess) {}

What’s the difference, is there any?

Hey there @n4kai!!

Currently you code does not throw any errors, though if I remember this project, there is quiet a bit that has to be done with another set of JavaScript provided by the project. What are you currently having trouble with?


function compareGuesses(humanGuess, computerGuess, targetGuess) {}

Is an older way of declaring a function, though still completely valid

Where as this one:

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

Is called arrow syntax, it is a newer version of function declaration that was introduced in version ES6. It contains several newer features making it easier to read and write. Here is a good bit on it:

As for the const keyword, you can actually use this on any funtion declaration. It adds a bit of security to your code by making sure a function name can’t be accidently, or purposefully, reassigned:

add = (n1, n2) => {  //without a const add can be reassigned
  return n1 + n2;
}

add = 2;

const multiply = (n1, n2) => { //However multiply can't be reassigned
  return n1 * n2;              //Because of the const
}

const can be used on a function declared with the function keyword, or a function declared with arrow syntax.

1 Like

Thanks for the function clarification.

As for the code, I got a bit lost. I pasted my code into https://www.codecademy.com/practice/projects/number-guesser-independent-practice, and also tried running it via index.html, which is included in the file pack for this task, yet the final game does not work in either case.

There are a few errors in your code causing to stop running. Your best friend when debugging is often the console.
Unfortunatly in this lesson it seems that Codecademy’s console is not displaying errors correctly, I am actually going to see about this being a bug. Fortunately you have developer tools.

Open your dev tools and then check the console for errors, you should see the first one when you click “Make a guess”.
Another thing you can do is use "console.log()` and see what is being returned by your functions.

1 Like

I’m not totally sure whether I do get your point, or not.
I’ve downloaded all of the provided files, put them in one folder, and run index.html via my default browser (FF).
Then I clicked “Make a guess” and examinated that element. On the bottom, in the console I guess, I found such error:

“Uncaught ReferenceError: math is not defined
generateTarget file:///D:/Codecademy/Number Guesser/script.js:8
file:///D:/Codecademy/Number Guesser/game.js:18”

Am I on the right track?

EDIT: So far I’ve found out that changing “math.” to “Math.” allowed computer guesses to display.
Still rounds do not update, and Target Number constantly equals zero.

Also why

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

works, and

generateTarget = () => {
  let randomNumber = Math.floor(Math.random()) *10;
  return randomNumber;
} 

Does not?

EDIT2: I found out that places where parenthesis are put makes the difference, however I’d like to rephrase my question, given the second function is fixed, would the first one always be preferred just because it’s shorter?

1 Like

Part of it may be personal preference. Personally I try and shorten my functions wherever possible.

generateTarget = () => {
  let randomNumber = Math.floor(Math.random() *10);
  return randomNumber;
} 

With this one, I don’t see the point in defining a variable just to then immediately return it, so in this case I would write:

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

or:

const generateTarget = () => Math.floor(Math.random() * 10);
2 Likes

ATM I wonder whether the second function is written down correctly. I’ve adjusted the first case to compare Math.abs instead of just comparing “humanGuess” and “computerGuess”, but I wonder if I got it right and with correct syntax.

compareGuesses = (humanGuess, computerGuess, secretNumber) => {
  if (Math.abs(secretNumber - humanGuess) === Math.abs(secretNumber - computerGuess)) {
    return true;
  }
  else if (Math.abs(secretNumber - humanGuess) > Math.abs(secretNumber - computerGuess)) {
    return false;
  }
  else if (Math.abs(secretNumber - humanGuess) < Math.abs(secretNumber - computerGuess)) {
    return true;
  }
};

Also, the console in my firefox returns error that “winner” is not defined.
Should I change:

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

into:

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

but I wonder if I got it right and with correct syntax.

Well you’ll know if you got it right if your program works, that’s the best feedback. Regarding on whether it’s in the right syntax. The syntax is right if it works, whether or not you can refactor it and shorten it is another question.

With your if…else if your first two statements cover most scenarios you can just use an else { } to cover all other statements.

I’d also recommend you start writing either function or const in front of your functions this will help any potential ambiguity.

Also, the console in my firefox returns error that “winner” is not defined.
Should I change:

Yes, as in the first instance, you’re referencing a variable winner but that’s not defined anywhere.

Also same thing here, you don’t really need that else if, just an else

2 Likes

Perfect, that may make it even easier to work.
You found that math was undefined, which was the first error I saw.

It seems you also found the error with your parenthisis, and your function should now be returning random numbers.


As for your 'winner' not defined error, both you and @munroe4985 are correct.
Since you are referencing that variable, you need to have it as input into your function.
If you look at game.js, you should see that it is calling updateScore with a arguement of winner, which is defined elsewhere in that file.

1 Like

Thanks for you help, I finally got my code up and running.

My, hopefully, last question is: Can I tune this code to be more cohesive, or is it readeable enough?

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, secretNumber) => {
  if (Math.abs(secretNumber - humanGuess) === Math.abs(secretNumber - computerGuess)) {
    return true;
  }
  else if (Math.abs(secretNumber - humanGuess) > Math.abs(secretNumber - computerGuess)) {
    return false;
  }
  else if (Math.abs(secretNumber - humanGuess) < Math.abs(secretNumber - computerGuess)) {
    return true;
  }
}

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

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

Looks great!

Only thing I can see that could be done differently, is what @munroe4985 stated earlier about else if blocks.

The last else if block in this particular case, can be changed to an else.

1 Like