Basic JavaScript: Chaining If Else Statements

Tell us what’s happening:

Not sure it I have a typo or whats wrong. Can someone please help.

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 {
return “Huge”;
}
}

return “Change Me”;
// Only change code above this line

// Change this value to test
console.log(testSize(19));


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 {
   return "Huge";
 }
}

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


// Change this value to test
console.log(testSize(19));

Your browser information:

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

Challenge: Chaining If Else Statements

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

Do you have an extra }?

I did yes but that did not work.

What is your current code and what do the failing tests say?

It’s important to pay attention to the errors that the console is providing you. When I paste your code in the error is:

SyntaxError: unknown: ‘return’ outside of function

This means that you are returning something outside of your function which shouldn’t be the case. Make sure you do not alter the base code and follow the instructions within the comments.

function testSize(num) {
  // Only change code below this line


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

i’ve read your message over and over. Do I have to get rid of the return function that says “Change me” because I did that. Not sure what else to try.

What is your current code and what do the errors and/or failing tests say?

1 Like

SyntaxError: unknown: Unexpected token (21:27)

19 |
20 | // Change this value to test

21 | console.log(testSize(19));
|

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 {
    return "Huge";
  }

    return "Change me";
  // Only change code above this line


// Change this value to test
console.log(testSize(19));

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

You changed code below the line that says “Only change code above this line”.
Specifically, you deleted a }. This means that your function has no closing brace and your code cannot run.

i did exactly as says in the video. What code goes down there?

I find it unlikely that the video told you to delete anything below that line. I told you what you deleted and why it causes a problem. You can fix it manually, or you can reset the code and write your solution again.

You are missing the function closing curly brace. Also that last return is outside of the function and its not needed.

1 Like

Wasn’t the last case supposed to be" else if (num >= 20 ) return “huge”? And then the last return right before the last curly brace :
else {return “change me”}

you don’t need to keep the return "Change me" line - it is saying to be change, you can delete that
you need to keep the curly bracket that was below the Change code only above this line

count your brackets, there should be an equal number of opening and closing ones