Number Guesser Code

Took me a minute of reading to find a significant difference. No reason you shouldn’t be able to do the same.
However, instead of reducing it to a text editing exercise, do a bit of programming, go investigate the incorrect behaviour and make observations about your code around how it gets to that behaviour.

I am sorry if I asked a dumb thing. I already tweaked around with the code and didn’t get the error.

Since I am a new user, I am not allowed to share my .js-file with you - but thank you very much for your response!

If you mean the missing () before the Math.abs()-expression - it’s an accidental omission in the screen shot which I already fixed but it still doesn’t execute as intended. Thank you very much for your advice and prompt answer, though!

i said copy paste your code, not share a JS file :wink: Otherwise there solutions like jsbin, jsfiddle or codepen.

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

// Random Target Number
const generateTarget = () => Math.floor(Math.random() * 10);

// Comparing Guesses with Target
const compareGuesses = (humanGuess, computerGuess, targetNumber) => {
  const humanDifference = Math.abs(targetNumber - humanGuess);
  const computerDifference = Math.abs(targetNumber - computerGuess);
  return humanDifference <= computerDifference;
};

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

// Next Round
const advanceRound = () => currentRoundNumber++;

That’s my code so far and I certainly don’t want to bug you with stupid questions I can solve myself! Maybe I starred at it already for too long and need to come back to it later, with fresh eyes. As to the error message in the console - I don’ get any, yet the round won’t advance.

i get the an error when i tried to save the code in the lesson:

const generateTarget = () => (Math.floor(Math.random() * 10);
                                                            ^
SyntaxError: Unexpected token ;

pretty helpful. What can we learn from this error? ; is unexpected? so what is excepted? maybe the line itself will hold some clues?

if this error doesn’t show in the terminal of the lesson, you can always use your browsers console

I probably don’t ever see questions the same way as they are thought of when someone types them out.
What I’m seeing here is a mismatch of expectations, are you supposed to be treating it like a word game, searching through the text? How carefully do you need to search? Not too obvious. So, how exact does the comparison need to be? Exact. Down to single characters being different. But are you supposed to do that here? Well, you can, it’s not hard. But also, no, that’s not a great approach, better to follow the behaviour.

Code is a bunch of instructions laid out just so. Similarly to the text comparisons, close isn’t what you’re going for, that’s not acceptable, that isn’t going to do the right thing.
So you need to be in full control of it, and if it’s different, then you need to be able to tell, this can be difficult if you don’t know that there is a problem, but when you do know the problem and you can measure it, then you can continue measuring those bad states until you reach the place it started.

How are you measuring problems? Well, you’re printing out the wrong answer aren’t you? So printing is the measurement.

The degree of control you need over code might not be easy to appreciate when codecademy is telling you what to write, sometimes even providing the exact text to write (again making it an editing problem instead of programming)

Doesn’t matter which difference it was when you can (and should) find them all.
Additionally you know there is a significant difference because you can run them both and get different results.
Making a perfect and reliable comparison is something you’d do with a text diffing program, not manually. However, this is a small amount of text and it can still be carried out without much difficulty.

I am sorry, I am really trying. As a german archaeologist, I didn’t have much overlaps with code development before and I love Codeacademy for all the comprehensible lessons I am getting. I’ll do better next time.

It’s not a complaint >_<
It’s just the difference in how you and I would approach it, or more to the point the difference between viewing it as a trivial problem and a confusing one.

Eyeballing both: Eh looks the same. This is confusing.

Character-wise comparison: See all differences


But that’s just when viewing it as text. As a program, the problem is instead:

Oh the result is wrong. What happened immediately before that, and was that wrong too? Repeat until found.

Oh the result is wrong. What can I do? Won’t find the problem (not looking, essentially)


Not knowing what to expect makes it legitimately confusing. But not because the problem is difficult!

Thanks very much, guys. I’ll keep that in mind!

By the way, I am getting warning messages in the editing program (Brackets) I am using. What is that all about?


How come humanScore, which is declared as a global variable at the top, is an unexpected token?

Brackets isn’t giving you those. JSLint and ESLint are their own programs, your editor just happens to be configured to invoke them on your code (it’s a text editor, it doesn’t do those things)

Those warnings/errors look like they refer to specific locations but the locations are not shown by your interface. Maybe it expects you to click them to bring you there.

As for what they’re complaining about, it’s that the code at that location is not valid javascript, does not match any patterns that the language defines.

Nope, clicking or redefinig them doesn’t help. What does javascript expect and what patterns does it define?

Syntax is patterns recognized by the language.
What does a while loop look like? That’s a pattern, isn’t it. And if you don’t match it, and also don’t match any other known pattern, then it isn’t javascript.

Your whole program needs to match a pattern for a valid javascript program which in turn is composed of many smaller patterns.

If it’s not valid javascript then you can’t run it so if you run it maybe you’d get a similar error from your language implementation (nodejs/firefox/chrome/whatever)

Or maybe the 1 means line 1. There’s also some code mentioned, so you could look for that too.

Or maybe those programs just aren’t used right, causing them to see something different from what you see.

Yes, I think 1 means line 1 since the variable humanScore is delared in line 1. Yet, why can’t I start the code by definig a variable?

You can.

Your environment is also something to be in control of, similar to code. Debugging somebody else’s environment (especially without even having access to it) isn’t such a great thing.

This is why you’d either use some kind of perfectly behaved environment (maybe brackets is that, how would I know) or you’d use fewer tools making it easier for them to be perfect and thus allowing you to be in control.
…Or you’d be in such control of the individual parts that even if they’re not perfect you’re still able to handle them (this is quite likely what brackets expects of you, at least to some degree)

The most likely cause is of course either that your code is wrong in some way or that you’re using your editor wrong, maybe you needed to save the file to make it re-run those linters, maybe you needed to configure something, or configured something you shouldn’t have.

You might think of it as that your editor tries to keep up with you, if you’re trying to keep up with your editor then the chain of command got turned up-side down. Can’t rely on your editor to be smarter than you.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.