What is wrong with my Code

let raceNumber = Math.floor(Math.random() * 1000);
let registerEarly = true ;
let runnerAge = 20;

if ( registerEarly && runnerAge > 18) {
raceNumber += 1000;
}

if (runnerAge && registerEarly > 18){
console.log(‘You will race at 9:30 am’);
console.log(Your race number is ${raceNumber}.);

}
else if (runnerAge && !registerEarly > 18 ){
console.log(‘You will race at 11:00am’);
console.log(Your race number will be ${raceNumber}.);
}
else if (runnerAge < 18) {
console.log(‘You will race at 12:30pm’);
console.log(Your race number will be ${raceNumber}.);
}

When I input runnerAge that is >18, no words comes out. I have been finding error for hours but I can’t find what’s wrong. I am new to JS please help

Take a closer look at your following conditional statements:

if (runnerAge && registerEarly > 18)
else if (runnerAge && !registerEarly > 18 )

Do you see the issue?

Hint

Make sure you’re comparing the right variables. Looks like you’ve swapped runnerAge and registerEarly.


For future posts, please format your code using the </> button.

I think I get what you mean now, thank you!
I would like to ask another question, why this code worked when I set

registerEarly = false
else if (runnerAge && !registerEarly > 18 )

the ! should return a true right? I don’t understand why it worked and give me the correct outcome

From my understanding the ! means reverse the boolean value, so it is actaully equals to

registerEarly= true 

That’s why i don’t understand why it give me the correct answer no matter I initially set registerEarly to either true or false

Yes, if registerEarly = false, then !registerEarly would return true.


If runnerAge = 20 and registerEarly = false, then the following conditional statement would evaluate to false and the code inside the else if block would not execute.

else if (runnerAge && !registerEarly > 18)

Did that answer your question? By the way, you might find this table listing operator precedence in JS useful.