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}.`)
}