Else If Statements Lesson is broken?

Tell us what’s happening:

As soon as I finish entering the edits to the code in order to pass the lesson, it tells me there is something wrong after the last character in the code. It points to the space at the end of the code and says there is a “SyntaxError: unknown: Unexpected token (11:14)”.

Has anybody else experienced this? Does anybody know how to fix it? It goes away if I delete the opening curly bracket to the right of the ‘else’ on line 6 but reappears as soon as I put it back in.

It’s infuriating, am I doing something wrong?

Your code so far


function testElseIf(val) {
if (val > 10) {
  return "Greater than 10";
} else if (val < 5) {
  return "Smaller than 5";
} else {
  return "Between 5 and 10";
}

// Change this value to test
testElseIf(7);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Introducing Else If Statements

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/introducing-else-if-statements

Hello and welcome to the forum :grin:!

The lesson works fine, it’s just that you’re missing a closing }:

function testElseIf(val) {
if (val > 10) {
 return "Greater than 10";
} else if (val < 5) {
 return "Smaller than 5";
} else {
 return "Between 5 and 10";
}
} // You're missing this one

// Change this value to test
testElseIf(7);

Thanks skaparate, this was driving me crazy!

1 Like

This is an error that you will encounter many times as you learn to code, so it’s good to learn what it means and how to find the problem. It means there is something wrong with your syntax, either you typed something that shouldn’t be there or that there is something missing and causing confusion.

In this case the error is pointing to the very end of your code. But that line looks fine, doesn’t it? So the problem must be elsewhere. Since the program thinks the problem is the end of your code, maybe you are missing something that should signal that a block of code has ended. Spoiler: You are missing a closing curly brace for your function.

2 Likes

@Acre Another way to easily spot this particular mistake is to indent your code. If you would have indented your original code (see below), then you would have had a better chance of spotting the missing } at the end of the function.

function testElseIf(val) {
  if (val > 10) {
    return "Greater than 10";
  } else if (val < 5) {
    return "Smaller than 5";
  } else {
    return "Between 5 and 10";
  }

// Change this value to test
testElseIf(7);

In general, indented code is much easier to read.

3 Likes

Hi Randel,

That’s a good shout. I’d taken all the indenting out because I thought I might have made a mistake with it that was causing the error. It does a good job of formatting the code well as you go along so I should be okay on that front for now.

Thanks for the advice,
Joe