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

I have the same problem, but with the boolean value. My code take the age in count, but not the true or false for early.

I write it like that :

let raceNumber = Math.floor(Math.random() * 1000);
let earlyRunner = false;
let ageRunner = 20;

if(earlyRunner = true && ageRunner > 18){
raceNumber += 1000;
}
if(earlyRunner = true && ageRunner > 18){
console.log('Votre course commençera à 9H30. Votre numéro de coureur est ’ + raceNumber + ‘.’);
}
else if(!earlyRunner && ageRunner > 18){
console.log('Votre course commençera à 11H00. Votre numéro de coureur est ’ + raceNumber + ‘.’);
}

Plus, I can’t set the variable early to const, because it send me an error message…

Here you’re not checking whether the earlyRunner is true, but you’re assigning true to earlyRunner, which doesn’t work as a condition.
You have to write

if(earlyRunner === true && ageRunner > 18)

Same for the other condition.

1 Like

Ah, ok ! I didn’t notice that…

Thank you !

A little bit pedantic, but an assignment (=) is an expression and will evaluate to the assigned value, as such it is a valid condition - it just isn’t the expected one. So in this case if(earlyRunner = true) will always be true, hence why it only takes age into account.

As and aside, as earlyRunner is a Boolean you don’t need the === because you are basically saying true === true or false === true. You can just take the first true or false (earlyRunner) and it will be the same.