Race day Challenge! using the switch format instead of (if, else if)

Hello everybody! so i got through the race day challenge project but i wanted to improve it a little bit because it seems like something fun to do and also (might inspire) anybody else who has weird thoughts like me haha or simply because it helps learning a little bit if you temper or move around a little bit with the code and syntax of stuff.

Soo… i have this code right? https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-control-flow/projects/race-day, i already tried to do something like, ok i have a let variable that depending on the runner’s age it varies the answer right? kind of like an eightBall project. so what happens if i want that entire sentence of code but with a switch format? like how could one go about doing something like that knowing that you have to add predetermined values to runners that are early and above age 18 > with +=1000 and also using the switch elements like pre-filling values with answers like with what i have in the if, else if case.

Sorry i believe i suck at explaining, hope the code helps a little bit this time in trying to figure out what i mean.

Hi, @snox22,

We’re super glad you shared the link with us, though we can only see our own code at that link, not yours. If you would be so kind as to post in raw form your actual code, it would be most appreciated. Then perhaps we can help with your problem.

2 Likes

Hi! sorry! that was totally my bad, here is the code i’m talking about:

let raceNumber = Math.floor(Math.random() * 1000);
const registeredEarly = true;
let age = 18;

if(registeredEarly && age > 18){
raceNumber += 1000;
}

switch (expression) {
case expression:

break;

default:

}

if (registeredEarly && age > 18){
console.log(Race will start at 9:30 am, your race number is ${raceNumber});
} else if (!registeredEarly && age > 18){
console.log(Race will start at 11:00 am, your race number is ${raceNumber});
} else if (age < 18){
console.log(Race will start at 12:30 pm, your race number is ${raceNumber});
} else{
console.log(‘Please go to the registration desk to view your race time’);
}

i want to use the switch function to override the (if, else if) conditionals, like for example how can i make it that it adds the corresponding +1000 to the number if the runner registered early and the age is over 18 and all the other conditionals about sending the message to the runners with only the switch conditional? or is this case not relevant?

There is only one possibility where the race number would be extended, so a switch is not the most suitable logic to use.

let raceNumber = Math.floor(Math.random()*1000);
let isAdult = runnersAge > 17;
let registeredEarly = true;
raceNumber += registeredEarly && isAdult ? 1000 : 0;

The above is very similar to if…else. You should be familiar with ternary statements, but if not, study it closely and see if you can refactor it to an if statement.