Race Day: Race start time changes correctly, but age doesn't impact the results!

Hello all.
I’ve already confirmed the correct answer for this project, but I’m curious why my original ideal only half works.

let raceNumber = Math.floor(Math.random() * 1000);
const registeredEarly = false;
let runnerAge = 14;

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

if (registeredEarly && runnerAge > 18) {
  console.log(`You will race at 9:30. Your race number is ${raceNumber}.`);
} else {
  console.log(`You will race at 11:00. Your race number is ${raceNumber}.`);
}

If I change the registeredEarly to true, the console displays You will race at 9:30 and adds 1000 to the race number. Likewise, if I change it to false, the time changes to 11:00 and 1000 is not added to the number. However, changing the runnerAge to a number lower than 18 doesn’t have any effect! Changing it to a number lower than 18 should prevent the code from run?

What is it about the correct solution that allows the age to impact what is displayed?
BTW, the correct code is apparently as follows:

if (registeredEarly && runnerAge > 18) {
  console.log(`You will race at 9:30. Your race number is ${raceNumber}.`) 
} 
else if (!registeredEarly && runnerAge > 18) {
  console.log(`You will race at 11:00. Your race number is ${raceNumber}.`)
}

Hi @hinokio2020
your else statement doesn’t have a condition. Therefore it is always executed if the upper condition isn’t fulfilled. The upper condition isn’t fulfilled if not registered early or the age is 18 or below or both.
The solution has also a condition for the else statement. Therefore, if any of the conditions aren’t fulfilled, there is no code to be executed. That would be the case if the age is 18 or below.

1 Like