Race Day

https://www.codecademy.com/courses/introduction-to-javascript/projects/race-day?action=resume_content_item

Hi, I have a question about Race Day task. My code looks like this:

let raceNumber = Math.floor(Math.random() * 1000);
let early = false;
let age = 25;
if (early = true && age >= 18) {
  raceNumber += 1000;
}
if (early = true && age >= 18) {
  console.log(`You start at 9:30 and your number is ${raceNumber}`);
}
else if (early = false && age >= 18) {
  console.log(`You start at 11:00 and your number is ${raceNumber}`);
}
else if (age < 18) {
  console.log(`You start at 12:30 and your number is ${raceNumber}`);
}

When I run it, it prints "You start at 9:30 … " although it should be 11:00. I saw on video that I can use !early insted of early = false but I would like to understand why I can’t do like I did?

Should be a comparison,

early === true

or, since early is a boolean, already,

if (early && age >= 18)

We can see that your code has three branches, so what if we begin with the simplest one, first?

if (age < 18) {

}

We can do away with checking age again in the other branches.

if (age < 18) {

} else if (early) {

}

and now we don’t need to check early again, either…

let early = false;
let age = 25;
let start;
if (age < 18) {
    start = `12:30`;
} else if (early) {
    start = `9:30`;
    raceNumber += 1000;
} else {
    start = `11:00`;
}
console.log(`You start at ${start} and your number is ${raceNumber}.`);

Granted it may not match the video, but if you compare the two you will see that we have the exact same conditions apply, with less code and verbosity.

1 Like

Thank you for help. However, when I change only early = true to early === true (and early === false), like on your first solution, it still doesn’t work.

1 Like

Try this code in your browser console…

 > if (false) {
       console.log("true branch followed");
   } else {
       console.log("else branch followed");
   }
   else branch followed
<- undefined

Notice which branch is followed?