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 G17mo 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
Wolle17mo 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 User17mo ago
Message Not Public
Sign In & Join Server To View
CodeNascher
CodeNascher17mo ago
and not copy paste from VS all the time?
You can also drag your js file into VSCode
Å Marlon G
Å Marlon G17mo 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
Wolle17mo ago
I guess this line: else (runnersAge === 18)gets ignored, so the line below it gets printed everytime.
CodeNascher
CodeNascher17mo ago
my guess as well
Å Marlon G
Å Marlon G17mo ago
But if I change the runnersAge to 18, then only that line is printed ...
Wolle
Wolle17mo ago
It gets printed no matter the input. Either put a if in there, or remove the assertion
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G17mo 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
CodeNascher17mo ago
else is the final condition. it has no check
Mannix
Mannix17mo ago
else isn't suppose to have a condition
Wolle
Wolle17mo 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 User17mo ago
Message Not Public
Sign In & Join Server To View
Mannix
Mannix17mo 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 User17mo ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G17mo 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 User17mo ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G17mo 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 User17mo ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G17mo ago
@omaj That's extremely helpful! Thank you very much! You win todays 🦚
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G17mo 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 User17mo ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G17mo ago
... oh, and final point then - this example also apllies to the switch operation and default in there ...
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon G17mo ago
Well then. Today I became more knowladgeable. Thank you again! You get another 🦚 😎
Want results from more Discord servers?
Add your server
More Posts
issues with image not fitting into a divI have a main div of 50% height. In which there are two divs each of a height of 47.5%. the child diGrid item content goes out of containerI am using MUI v5 Grid for my project. I wanted to line up 5 items but when I checked for responsivenew branch or repository for different tech stack implementation of same siteHey everyone, I been working on my site with plain HTML, CSS, and JS so far and I've implemented a dIs it worth the effort to ensure websites are responsive at extremely small screen sizes?Hello guys, I hope you're all doing well! 👋 I need your advice on something that's been on my minProblem on making class active on clickhttps://codepen.io/GiteenShiro/pen/dyqOgjp pen of my code. When I hover on the image, a dropdown kini want to move this to the end of the orange block. how should i do that?note: with JS i can change width of the orange block. So the pointer has to be at the end of the oraHSL and class number systemwhy that number system; the hue, saturation and lightness become larger in the colors, but clr numbeNeed help with creating a progress barIm trying to add a small triangle to the marked div but im having issues with it. CAn someone help mImage is not centeredPlease tell me what is the error, I can’t center the image, margin 0 is ignored and does not work htMy life has been blocked by CORS policy: Response to preflight request doesn't pass accessWho doesn't like a bit of monday morning cors? 😆 Was hoping that someone could help me narrow dow