Confusing syntax issue with "Raceday Project" in introduction to javascript

Hello, I’m new to Javascript and coding in general. I’ve encountered a strange syntax error, and I was hoping someone could help me understand what I did wrong. When I run the code below, I get:

/home/ccuser/workspace/learn-javascript-U2P2/main.js:17
} else (runnerAge === 18) {
^
SyntaxError: Unexpected token {

From the “Raceday Project” https://www.codecademy.com/courses/introduction-to-javascript/projects/race-day

I’m not sure what the issue is, so I assume I’m blind, illiterate, or just not knowledgeable enough about javascript formatting. The error points to the bracket for the block, but what’s the actual issue? Doesn’t the logic need the bloc to know what is being output from the else statement? It’s very confusing. I can “fix” it, by adding an If after the else, then it outputs in the console perfectly fine, but that doesn’t make sense with the previous lessons on conditionals. I couldn’t find this specific error mentioned anywhere on the forums, so I would appreciate any assistance. Also I hope I formatted this appropriately.

[codebyte language=javascript]
let raceNumber = Math.floor(Math.random() * 1000);

let registeredEarly = true

let runnerAge = 19

if (registeredEarly === true && runnerAge >= 18) {
  raceNumber += 1000
}

if (registeredEarly === true && runnerAge > 18) {
  console.log(`Runner number ${raceNumber} will race at 9:30 A.M.`);
} else if (registeredEarly ==! true && runnerAge > 18) {
  console.log(`Runner number ${raceNumber} will race at 11:00 A.M.`);
} else if (runnerAge < 18) {
  console.log(`Runner number ${raceNumber} will race at 12:30 P.M.`);
} else (runnerAge === 18) { 
  console.log(`Runner number ${raceNumber}, please report to the registration desk.`);
}

// ignore the below conditionals, it's for testing.
/*if(runnerAge === 18) {
  console.log('yes');
} else (runnerAge !== 18) {
  console.log('no');
}

let sale = true;

sale = false;

if(sale) {
  console.log('Time to buy!');
} else {
  console.log('Time to wait for a sale.');
}*/
[/codebyte]

Notice how you’ve got a condition that comes after the else?

} else (runnerAge === 18) { 
        ^^^^^^^^^^^^^^^^^

This will be throwing the error, because it doesn’t make sense to the JavaScript compiler. else is meant to be “all other eventualities,” so putting a condition after it is opposite to what else is meant to do.

Hmm, ok. I have a hard time modeling this logic in my mind. Else = “all other eventualities”, so the error is because I have a specific condition after the else, which defeats the purpose of else? I’m still confused, but I’ll do some research on how else is suppose to work, and how the logic is suppose to play out. I appreciate the swift help though, thank you for pointing me in the right direction.

Imagine I tell you: if there’s ten or less people coming for dinner (if condition), cook dinner. If any other number of people come, if there’s 11 people, order takeaway (else with a condition). That’s confusing, and it isn’t really clear what I’m telling you to do. The way JavaScript handles that is by throwing an error; you tell me “well you can organise food instead.”

Ok, this makes more sense. So, “Else” in this case means essentially anything else, which would imply that any further specific conditions wouldn’t make contextual sense. Then my only question after this, what is the correct way to apply the runnerAge === 18 condition? Make it an “Else if” statement? That works on the console.

1 Like

Exactly! Happy coding!