Number Guesser - please help, it's driving me mad! lol

My code for Number Guesser

Can anyone tell me where I’m going wrong? The compare guesses bit has loads of bugs apparently, it’s new thing is saying 'unexpected token return. I thought return was needed here?

full error message as follows, If anyone has any ideas i’d be very grateful

Julie

/home/ccuser/workspace/independent-practice-js-number-guesser/script.js:11
if (Math.abs(target - humanGuess) < Math.abs(target - computerGuess || Math.abs(target - humanGuess) === Math.abs(target - computerGuess)) return true;
^^^^^^
SyntaxError: Unexpected token return
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Hey Julie,

So this is your code:

if (
  Math.abs(target - humanGuess) < Math.abs(target - computerGuess 
  || Math.abs(target - humanGuess) === Math.abs(target - computerGuess)
) 
  return true;
} else { return false;
}

What happens if you add an opening curly bracket after the if statement?

if (your condition) {
  return true;
} else {
  return false;
}

?

1 Like

There’s also an unclosed set of parentheses in the if statement itself, that I can see; though I’m unsure whether that’s something the JS scripting engine would throw an error for, or just something which would quietly affect the evaluation…

Hey there! Thanks for the help!
Not a lot happens, it just gives me an error for the new bracket… even when I add another one at the bottom to balance it.

Thank you so much both of you, a combo of the 2 has sorted my error message out!

However, when I enter a number into the browser it doesn’t do anything. Do I have to console.log or should it just returning as is?