Hello newbie Question!

Hello everyone new to coding! I’m loving it so far I bumped into quite a bit of issues when coding that I would love some help with!

I am doing the JavaScript conditionals module on Race Day and I got my code to work, but I noticed an issue.
I got the code to work with this format:
let raceNumber = Math.floor(Math.random() * 1000);

const registerEarly = true;

const age = 19;

if (age > 18 && registerEarly) {console.log(raceNumber += 1000);}

if (age > 18 && registerEarly) { console.log(you will race at 9:30am #${raceNumber});

} else if (age > 18 && !registerEarly) { console.log(you will race at 11:00am #${raceNumber} );

} else if ( age < 18 ) { console.log(you will race at 12:30pm #${raceNumber});

} else if (age === 18 && registerEarly || age === 18 && !registerEarly) { console.log(‘see registration desk’); }

But when I went to change the last else if to else like this:

et raceNumber = Math.floor(Math.random() * 1000);

const registerEarly = true;

const age = 19;

if (age > 18 && registerEarly) {console.log(raceNumber += 1000);}

if (age > 18 && registerEarly) { console.log(you will race at 9:30am #${raceNumber});

} else if (age > 18 && !registerEarly) { console.log(you will race at 11:00am #${raceNumber} );

} else if ( age < 18 ) { console.log(you will race at 12:30pm #${raceNumber});

} else (age === 18 && registerEarly || age === 18 && !registerEarly) { console.log(‘see registration desk’); }

The code won’t work, I even tried to use switch statements to make my code work like:
let raceNumber = Math.floor(Math.random() * 1000);

const registerEarly = true;

const age = 19;

switch (age && regiserEarly)

case (age > 18 && registerEarly) :

console.log(you will race at 9:30am #${raceNumber});

break;

case (age > 18 && !registerEarly) :

console.log(you will race at 11:00am #${raceNumber} );

break;

case ( age < 18 ) :

console.log(you will race at 12:30pm #${raceNumber});

break;

default:

console.log(‘see registration desk’);

and that won’t work either.

Can someone please explain to me why my else statement at the end won’t work, and why my switch won’t work either? Thanks ahead of time!

Formatting your code using the </> will make it more readable for someone trying to help; please edit your post. Welcome to the forums!

2 Likes

As @dr_victoria said, formatting would help! However, the one thing with the else statement is you can’t have a condition with it:

The whole point of else is to cover every other case-which have a condition would mean it isn’t covering every other case!

3 Likes

currently its not letting me edit it at this time, I will do that for next time

Just to be clear its this?

let raceNumber = Math.floor(Math.random() * 1000);

const registerEarly = true;

const age = 19;

Thank you for the help its much appreciated! Ok so my follow up question is how would you enter that in as a switch instead of a if - else statement?
Also I will remember to add This type of text next time I enter code

1 Like

The Mozilla Dev docs has a good example of how to use switch.

1 Like