Basic JavaScript - Chaining If Else Statements

I solved most of the challenge before getting this error and couldn’t run tests:

“SyntaxError: unknown: Missing semicolon. (11:20)”

I fixed the error and added the semicolon it asked for, but why is the semicolon necessary in the “else if chain” and after the parentheses of all places?

Thanks in advance.

Your code so far

function testSize(num) {
  // Only change code below this line
 if (num < 5) {
    return "Tiny";
  } else if (num < 10) {
    return "Small";
  } else if (num < 15) {
    return "Medium";
  } else if (num < 20) {
    return "Large";
  } else (num >= 20); {
    return "Huge";
  }

  return "Change Me";
  // Only change code above this line
}

testSize(7);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0

Challenge: Basic JavaScript - Chaining If Else Statements

Link to the challenge:

What’s going on here?

I see something that does not belong on this line. Do you?

I would say it’s the semicolon. I tried I first wrote it without, and then it gave me the error and asked for one. I couldn’t run tests without it.

the function goes down the whole chain of if else statements, this is the last in the chain. If the number is greater or equal than 20, it returns a value of “Huge”.

I’m not sure why it won’t function without the semicolon after the brackets though. Do you?

The semicolon is a problem, but you have two problems on that line.

if (condition1) {
  statement1
} else if (condition2) {
  statement2
} else if (condition3) {
  statement3
} else {
  statement4
}

What is here in the example?

well I just noticed one thing that is not there, (condition4) to match statement4. And thats fine.

was it unecessary to give that parameter to my else statment? Meaning was (num >= 20) one of my errors? Could I have simply wrote:

} else {
  return "Huge";
}

Edit: I ran the test using the above. It worked.

So with my previous “solution”, it seemed to have worked but was poorly written code. By removing the final parameter I would have had a much better solution.

It’s still strange that a semicolon made it function.

Else statements do not have conditions (the part you see with if and elif statements between ( and )).

This is equivalent to

  } else {
    num >= 20;
  }
  {
    return "Huge";
  }