Help with my Javascript Adventures
Go to solution
Solved by FezBoy,
The way switches.are evaluated is different to that of ifs. You shouldn't have foo===bar in a case, you would do switch (Foo){case: bar}
This unfortunately make it difficult to check if something is positive, negative, or zero. I think that an if else chain would be your best bet.
Scratch that, try this:
Quoteswitch (Math.sign(sleepDebt)) { case 0: return('You got the perfect amount of sleep!'); case 1: return('You should get some rest!'); case -1: return('You got more sleep than needed!'); default: return('Check your code dude!'); }
Math.sign(foo) returns -1 for foo<0, 0 for foo===0, and 1 for foo>0. The switch statement compares that to 0, -1, or 1 to determine which path to take.
(Please excuse mobile formatting)
See the MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now