Raceday Javascript My Code isn't working for some reason

let raceNumber = Math.floor(Math.random() * 1000);
let early = 'false'
let runnerAge = 19
if (early && runnerAge > 18) {raceNumber += 1000
} 
else if (early && runnerAge > 18) 
{ 
  console.log(`Racetime is 9:30 am and race number is: ${raceNumber}.`)
} 
else if (!early == false && runnerAge > 18 ) {
  console.log(`Late adults run at 11:00 am and your race number is: ${raceNumber}`)
} 
else if (runnerAge < 18) 
{
  console.log(`Youth registrants run at 12:30 and your race number is: ${raceNumber}`)}
else {
  console.log('Please approach the registration desk')
}

It’s still the same reason that @mtrtmk pointed out in your last post. Your variable early is a string in your code and therefore !early will be falsy:

2 Likes

Furthermore, for some reason, you have made a number of other changes to your code compared to the code you posted in the earlier thread.

Here are a few issues to consider in your latest code:

  • As mirja_t noted, early is supposed to be a boolean.
let early = 'false' // Incorrect
let early = false // Correct
  • You have combined two if statements which are actually meant to be separate.
// You wrote:
if (early && runnerAge > 18) {
    raceNumber += 1000
} else if (early && runnerAge > 18) { 
   console.log(...

// It should be:
if (early && runnerAge > 18) {
    raceNumber += 1000
} 

if (early && runnerAge > 18) { 
  console.log(...

There should be no else between the two if statements. As mentioned in Step 5 of the instructions,

Create a separate control flow statement below the first (starting with if again). This statement will check age and registration time to determine race time.

  • In one of the else if conditions, you wrote:
// You wrote:
else if (!early == false && runnerAge > 18 ) {

// It should be:
else if (early == false && runnerAge > 18 ) {  // Acceptable
// or better still
else if (!early && runnerAge > 18 ) {   // BETTER
3 Likes

Thanks! I was able to see my mistake and I think it’s because I don’t look at my code closely enough to notice any differences.

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