Day race Javascript Question

I did the exercise like this. It’s not very clear why i need to write raceNumber+=1000 to the first statement. Somebody can explain me why?

let raceNumber = Math.floor(Math.random() * 1000);
const earlyRegistered = true;
const Age = 18;
if (Age > 18 && earlyRegistered) {
raceNumber+=1000; (this line)
}
if (Age > 18 && earlyRegistered === ‘true’) {
console.log('Partecipant ’ + raceNumber +‘th the race will start at 9:30 am’);
} else if (Age > 18 && earlyRegistered === false) {
console.log('Partecipant ’ + raceNumber +‘th the race will start at 11:00 am’);
} else if (Age < 18) {
console.log('Partecipant ’ + raceNumber +‘th the race will start at 12:30 pm’);
} else {
console.log(‘Approach the registration’)
}

Race numbers are often divided over the different distances, so for example 1 till 1000 could be 5km, 1000 till 2000 could be 10km and 2000 till 3000 could be for 21km

It seems in this exercise pre-registration are thousand and higher, while people who sign up on the day itself get the number 1 till 1000.

1 Like

So it means that the code will take just the first 1000 people right?

no, it means if you did pre-registration your start number will be higher then 1000 (given we increased it with 1000)

1 Like

but when i try it the number that comes out is within 0 and 1000, not higher than 1000

here it’s the exercise:
Here’s how our registration works. There are adult runners (over 18 years of age) and youth runners (under 18 years of age). They can register early or late. Runners are assigned a race number and start time based on their age and registration.

Race number:

  • Early adults receive a race number at or above 1000.
  • All others receive a number below 1000.

Start time:

  • Adult registrants run at 9:30 am or 11:00 am.
    • Early adults run at 9:30 am.
    • Late adults run at 11:00 am.
  • Youth registrants run at 12:30 pm (regardless of registration).

But we didn’t plan for runners that are exactly 18! We’ll handle that by the end of the project.

age variable is set to 18, while the condition checks for age greater then 18

1 Like

so it means if i am over 18 and i register early i will have a number over 1000
otherwise i will have a number within 1000
is that right?

1 Like

yes, that is right.

1 Like

Ok thank you @stetim94 :smiley:

1 Like

what is gonna be the correct syntax ? worked in both way :nerd_face:
#1
if ((registredEarly) && (runnerAge > 18)){ }

#2

if (registredEarly && runnerAge > 18){ }

Both are fine, but the extra parenthesis in #1 are unnecessary as evidenced by the fact that #2 works just as well. I’d leave them out.

second is kinda JAVA syntax the first is JS syntax?

Either syntax is pretty universal for many languages (JavaScript, Java, C++, C# & maybe more). Like I said, I would leave the extra parenthesis out because they aren’t needed, but it’s really programmer preference. If you had complex expressions on either side of the &&, you might add the parenthesis for readability, but they’d still be unnecessary.

1 Like