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?
let raceNumber = Math.floor(Math.random() * 1000);

const earlyRegister = true;
let runnersAge = 16;

if (runnersAge > 18 && earlyRegister) {
raceNumber += 1000;
}

if (runnersAge > 18 && earlyRegister) {
console.log(`Hello, runner number ${raceNumber}. You're start time is 9.30 am.`);
} else if (runnersAge > 18 && !earlyRegister) {
console.log(`Hello, runner number ${raceNumber}. You're start time is 11.00 am.`);
} else if (runnersAge < 18)
console.log(`Hello, runner number ${raceNumber}. You're start time is 12.30 am.`);
else (runnersAge === 18)
console.log(`Hello! Since you're 18, please see the registration desk.`);
let raceNumber = Math.floor(Math.random() * 1000);

const earlyRegister = true;
let runnersAge = 16;

if (runnersAge > 18 && earlyRegister) {
raceNumber += 1000;
}

if (runnersAge > 18 && earlyRegister) {
console.log(`Hello, runner number ${raceNumber}. You're start time is 9.30 am.`);
} else if (runnersAge > 18 && !earlyRegister) {
console.log(`Hello, runner number ${raceNumber}. You're start time is 11.00 am.`);
} else if (runnersAge < 18)
console.log(`Hello, runner number ${raceNumber}. You're start time is 12.30 am.`);
else (runnersAge === 18)
console.log(`Hello! Since you're 18, please see the registration desk.`);
29 Replies
Å Marlon G
Å Marlon G2y ago
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?
Wolle
Wolle2y ago
Your last else has an assertion, but no if. That may cause the problem. Do you have any errors in your console?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
CodeNascher
CodeNascher2y ago
and not copy paste from VS all the time?
You can also drag your js file into VSCode
Å Marlon G
Å Marlon G2y ago
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 code
Wolle
Wolle2y ago
I guess this line: else (runnersAge === 18)gets ignored, so the line below it gets printed everytime.
CodeNascher
CodeNascher2y ago
my guess as well
Å Marlon G
Å Marlon G2y ago
But if I change the runnersAge to 18, then only that line is printed ...
Wolle
Wolle2y ago
It gets printed no matter the input. Either put a if in there, or remove the assertion
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G2y ago
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
CodeNascher
CodeNascher2y ago
else is the final condition. it has no check
Mannix
Mannix2y ago
else isn't suppose to have a condition
Wolle
Wolle2y ago
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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mannix
Mannix2y ago
if (runnersAge > 18 && earlyRegister) {
console.log(`Hello, runner number ${raceNumber}. You're start time is 9.30 am.`);
} else if (runnersAge > 18 && !earlyRegister) {
console.log(`Hello, runner number ${raceNumber}. You're start time is 11.00 am.`);
} else if (runnersAge < 18)
console.log(`Hello, runner number ${raceNumber}. You're start time is 12.30 am.`);
else {
console.log(`Hello! Since you're 18, please see the registration desk.`)};
if (runnersAge > 18 && earlyRegister) {
console.log(`Hello, runner number ${raceNumber}. You're start time is 9.30 am.`);
} else if (runnersAge > 18 && !earlyRegister) {
console.log(`Hello, runner number ${raceNumber}. You're start time is 11.00 am.`);
} else if (runnersAge < 18)
console.log(`Hello, runner number ${raceNumber}. You're start time is 12.30 am.`);
else {
console.log(`Hello! Since you're 18, please see the registration desk.`)};
that's how it suppose to look
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G2y ago
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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G2y ago
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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G2y ago
@omaj That's extremely helpful! Thank you very much! You win todays 🦚
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G2y ago
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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G2y ago
... oh, and final point then - this example also apllies to the switch operation and default in there ...
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G2y ago
Well then. Today I became more knowladgeable. Thank you again! You get another 🦚 😎
Want results from more Discord servers?
Add your server