# Race Day Project

Greetings everyone!
I’ve just wrote a code for the Project Race Day from the Javascript Lessons:

I would like to post my code for someone to check it and answer me the two following questions :

1. I used an if - else if statement, but if you check , you will see that on the first two I used two conditions (age and register), on the third I used only one (age), and on the last one I assumed it uses as a condition age = 18 which seems correct by running the code. My question is, is it really ok to use less conditions for some else if, since I started with more and end it up with less? The code runs just fine but I was curious.

2. My second question is that the code gives us a random number generator from 0 to 999. We hypothetically give a number to every runner. But since the numbers are random how we check if the code gives the same number to more than one people? Because they don’t clarify it at the problem and even though the probabilities are low it can still happen.

For anyone who will take the time to answer, thank you in advance.

Here is my code :

let raceNumber = Math.floor(Math.random() * 1000);
let register = true;
let age = 23 ;
let runner ;

if (age > 18 && register === true)
{
register = ‘early’;
raceNumber = raceNumber + 1000 ;
raceTime = ‘9:30 am’;
console.log(`Your race number is: \${raceNumber}. You will race at \${raceTime}`);
}
else if (age > 18 && register !== true)
{
register = ‘late’;
raceTime = ‘11:00 am’;
console.log(`Your race number is: \${raceNumber}. You will race at \${raceTime}`);
}
else if (age < 18)
{
runner = ‘Youth runner’;
raceTime = ‘12:30 pm’;
console.log(`Your race number is: \${raceNumber}. You will race at \${raceTime}`);
}
else
{
console.log(‘Visit the registration desk’);
}

One way to ensure there is no duplication is to store the numbers in an array and check each number against the array. If not present, then it is a good number. Add it to the array, and then assign it accordingly.

Given this is a lesson on conditionals, it is quite possible there are not really very many tools in our kit, to this point, arrays being one of them. If that is in fact the case, then the question becomes moot and we are left to fall back on probability. The probability of generating the same number 1 to 999 twice in say a few dozen tries is rather slim. Not improbable, but slim enough for our purposes, here.

1 Like

Thank you so much for your immediate response! Makes sense! I guess it’s because we haven’t started the arrays course yet. If I may ask, any insight regarding my first question?

1 Like

Again, we need to consider whether logical operators have already been introduced. We should not be using tools that are not part of our learning, thus far. However, if logical operators have been introduced, then by all means use them, unless you can find some simpler approach. The key is to write conditionals as simply as possible. They are essentially yes/no questions.

Yes they are! To be more specific what I mean is:

I start my If - else if statement like this

if (age > 18 && register === true)

We see that inside the parenthesis I used age> 18 && register === true.

Then we have

else if (age > 18 && register !== true)

I used again inside the parenthesis the conditions age and register

On the third else if though I wrote

else if (age < 18)
We see here that I used only the age condition and not the register variable too! So what I want to say is, is it ok to continue an if- else if with less conditions like in this example? (Some if - else if have both age and register variables and some only age.

As far as I know, it is perfectly okay. However in this example, you could replace the last else if part of the statement with just an else statement.
Changing the amount of conditions causes no errors and, as far as I know, is not considered bad practice.

Thank you so much for clarifying, I appreciate it!