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.

2 Likes

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.

2 Likes

Hello, my code is working properly except my race number doesnt come out as its supposed to when the runner has registered early and is an adult. instead of just adding 1000 to the race number, it puts the race number and then 1000 to the end of it (ie. 1511000 or 2641000)

Here is my code:

let raceNumber = Math.floor(Math.random() * 1000);

let earlyRegister = true

let runnerAge = 18

if (earlyRegister === true && runnerAge >= 18) {

console.log('Your race number is ’ + raceNumber + 1000)

}

if (earlyRegister === true && runnerAge >= 18) {

console.log(‘Your race will start at 9:30am’ +’ ’ +‘Your race number is’ + ’ ’ + raceNumber + 1000)

} else if (runnerAge >= 18 && earlyRegister === false) {

console.log('Your race will start at 11:00am. ’ + 'Your race number is ’ + raceNumber)

} else if (runnerAge < 18) {

console.log('If you are under 18, your race will begin at 12:30pm. Your race number is ’ + raceNumber)

}

It has to do with the way you are concatenating the values. Javascript will attempt to convert everything to a string when you are adding values to a string. So the statement below will take the random raceNumber convert it to a string and concatenate it with the string 1000.

You can avoid this by either doing the math prior to concatenating with the rest of the string

raceNumber += 1000;
console.log('Your race will start at 9:30am' +' ' +'Your race number is' + ' ' + raceNumber);

By using backticks instead:

console.log(`Your race will start at 9:30am.  Your race number is ${raceNumber + 1000}`)

Or by adding parenthesis around the numbers so that JS will do the math first and then concatenate the result:

console.log('Your race will start at 9:30am' +' ' +'Your race number is' + ' ' + (raceNumber +1000));

All three methods will provide the same result.

1 Like

Oh wow! Thank you so much! I come from a math background so the parenthesis made a lot of sense! I’ll stick with that! I appreciate you!

Hi I have the same problem but cant see the solution

let raceNumber = Math.floor(Math.random() * 1000);

const registeredEarly=true;

const age=82;

raceNumber=1000;

if ( age > 18 && registeredEarly === true) {

raceNumber +=1000;

}

if( age >= 18 && registeredEarly === true){

console.log(Race Will start 9:30 am and your number is ${raceNumber});

} else if (age >= 18 && registeredEarly === false){

console.log(Race Will start 11:00 am and your number is ${raceNumber});

} else if (age < 18){

console.log(Race Will start 12:30 am and your number is ${raceNumber});

}

Hello,
You can see my solution and tell me if there is anything wrong in it.

let raceNumber = Math.floor(Math.random() * 1000);

var regEarly = true;

var runnerAge = 21;

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

if(runnerAge > 18 && regEarly){
  console.log(`Your Adult registrants run is at 9:30 am and your race number is ${raceNumber}`);
} else if (runnerAge > 18 && regEarly === false){
  console.log(`Your Adult registrants run is at 11:00 am and your race number is ${raceNumber - 1000}`);
} else{
  console.log(`Your Youth registrants run is at 12:30 pm and your race number is ${raceNumber}`);
}