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