JS – why is it printing?
Hi!
Just taking my first babysteps into JS with codecademy.
Could someone tell me why the
else
statement prints in this example when the runnersAge
is set to anything except 18?
29 Replies
And ... is there a codepen equivalent for JS, where you can just paste an URL for these types of questions and not copy paste from VS all the time?
Your last else has an assertion, but no if. That may cause the problem. Do you have any errors in your console?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
and not copy paste from VS all the time?You can also drag your js file into VSCode
No, no errors. This is what is printed:
Hello, runner number 825. You're start time is 12.30 am.
Hello, runner number 825. Please see the registration desk for starting time.
How do you 'run' the code in codepen? I get the console, but no run button if I want to test changes to codeI guess this line:
else (runnersAge === 18)
gets ignored, so the line below it gets printed everytime.my guess as well
But if I change the
runnersAge
to 18, then only that line is printed ...It gets printed no matter the input.
Either put a if in there, or remove the assertion
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Aha ... So then I'll maybe use JSFiddle when I only have JS questions since arun button is nice then, and auto is noce for HTML and CSS. 👍
Ok. So since I'm a total newbie. What's the assertion? This articel is a bit overwhelming to be honest ... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Assertions
else is the final condition. it has no check
else isn't suppose to have a condition
I mean the thing inside the if brackets. (is assertion the right word for that? sry, english isn't a language I use everyday)
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
that's how it suppose to look
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Yes! Removing the else condition works, and obvioulsy makes sense.
This is what is challenging ... you have to explain all possibilities, but at some point you don't. When is that point? 😅
With time ...
Thank you so much!
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Aha ... that is not explained in the course.
So then the last condition would be like this?
else if (runnersAge === 18) {
console.log(
Hello! Since you're 18, please see the registration desk.)};
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
@omaj That's extremely helpful! Thank you very much! You win todays 🦚
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Trying to write it concise. Is this a correct summary?
End with an else if you always want a final outcome but don't necessarily need a condition matching your values, and else-if if you want to see all conditions in play
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
... oh, and final point then - this example also apllies to the switch operation and
default
in there ...Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Well then. Today I became more knowladgeable. Thank you again! You get another 🦚
😎