Race Day Java Excercise


#1

The console logs a correct value if the age is older than 18. If I change the age to under 18 nothing changes. I cannot find the mistake.

let raceNumber = Math.floor(Math.random() * 1000);
let raceRegisteredEarly = true;
let runnersAge = 8;

if (runnersAge => 18 && raceRegisteredEarly === true) {
raceNumber = (raceNumber + 1000);
}

if (runnersAge => 18 && raceRegisteredEarly === true) {
console.log("Race will start 9:30 am and your number will be " + raceNumber);
}
else if (runnersAge > 18 && raceRegisteredEarly === false) {
  console.log("Late adults run at 11:00 am and your number will be "+ raceNumber);
}
else if (runnersAge < 18) {
  console.log("Youth registrants run at 12:30 pm (regardless of registration) and your race number will be " + raceNumber);
}

#2

if (runnersAge => 18 && raceRegisteredEarly === true) {
raceNumber = (raceNumber + 1000);
}

if (runnersAge => 18 && raceRegisteredEarly === true) {
console.log("Race will start 9:30 am and your number will be " + raceNumber);
}

Remove the equal sign here “=>”. This should be a simple “>”


#3

for comparison we have greater then (>) and greater then or equal to (>=), you seem to have swapped > and = when attempting to use greater then or equal to


#4

Thank you, that did the trick. But should it not also work that you define a group older or equal 18 and another group that is younger than 18? That was my original intent.


#5

if you want older (greater) then or equal to 18, use the correct comparison operator. See my earlier answer.


#7

The project is asking for four different categories.

The First is Runners older than 18 & registered early. For this, you need to use the comparison operator >
The second is Runners older than 18 but did not register early. This still uses the comparison operator >
The third is runners under the age of 18 regardless of registration time. For this you will need to use <
The fourth is runners 18 years of age regardless of registration time. For this, you just use the else statement

You should have a final else statement like the following:
else{
Your code in here. (Note the runner is 18 years of age they will naturally fall into this else statement as they do not meet the requirements of the if or else if statements)
}


#8

Yeah you are right, I didn’t really read it right. I just have to keep doing more exercises I guess.
Thanks!


#9

Not a big deal, it is quite easy to miss something obvious in my experience. Hope it runs correctly for you!


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.