Race Day: Why does my code return a race time when I make runnerAge = boolean?

Hi! I just completed the Conditionals - Race Day exercise and I decided to play around with the values of my variables. Everything worked as expected until I let runnerAge = true. Rather than logging “See the registration desk.” it logged “Your race starts at 12:30PM. Your race number is 910.” which is the result I would expect for entrants under 18. Same thing happens when I let runnerAge = false.

let raceNumber = Math.floor(Math.random() * 1000); let earlyBird = true; // variable indicating runner's age let runnerAge = true; if(earlyBird && runnerAge > 18) { raceNumber += 1000; } else { raceNumber; } if(earlyBird === true && runnerAge > 18) { console.log(`Your race starts at 9:30AM. Your race number is ${raceNumber}.`); } else if(earlyBird === !true && runnerAge > 18) { console.log(`Your race starts at 11:00AM. Your race number is ${raceNumber}.`); } else if (earlyBird === true || earlyBird === false && runnerAge < 18) { console.log(`Your race starts at 12:30PM. Your race number is ${raceNumber}.`); } else { console.log(`See the registration desk.`) }

When I changed runnerAge to 18, or to a string, it logged “See the registration desk” as I expected. Why does entering a boolean for runnerAge do this?

It prints that because when you set runnerAge to a boolean it just ignores the comparison to the number and just looks at if runnerAge is true are false.

1 Like

I think that Javascript treats false as 0 and true as 1
for numerical computations or comparisons.

let x = true; x = x + 1; console.log(x); const y = true; console.log(y < 2);
2 Likes

That explains why it comes up with the under-18 text! Thank you so much :slight_smile: