Any tips on how to improve this code?

I just completed my assignment on conditionals for JavaScript. Everything worked but I was wondering if someone could have a look at it and tell me where there’s room for improvement to not only shorten the amount written but also make it easier. I’m always up for a bit of constructive criticism :slight_smile: If you don’t want to know how to do the assignment, don’t read this. Thanks

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

let early = false;

let age = 18;

if (early && age > 18) {

raceNumber += 1000;

} if (early && age > 18) {

console.log(`You will be racing at 9:30 am. Your race number is ${raceNumber}`);

} else if (!early && age > 18) {

console.log(`You will be racing at 11:00 am. Your race number is ${raceNumber}`);

} else if (age < 18) {

console.log(`You will be racing at 12:30 pm. Your race number is ${raceNumber}`);

} else{

console.log('Please go to the registration desk');

}

Hello, @galds.

It would be skipping a step from the project instructions, but consider how you could achieve the same results without using the same exact if statement twice:

Hint:

You can simply use a single if...else if...else if...else structure.

1 Like

Thanks for the reply. Yeah now that I think of it, after I add 1000 to the race number, I could just continue from there within that if block instead of creating another if statement. Thanks :slight_smile:

1 Like