Chaining if Else. Pls Help

Can i get help to get this script to return the appropriate string for each condition

function testSize(num)  {
 
if (num  <  5) {
  "Tiny";
}  else if  (num  <  10) {
  "Small";
}  else if  (num  <  15) {
  "Medium";
}  else if  (num  <  20) {
  "Large";
}  else  (num >= 20) {
  "Huge";
}

  return "Change Me";
}

testSize(7);

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.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

You have to use the return statement to return the words like “Huge”.
Also the last else statement doesnt require a condition.

Hi @redeemerkyler, what exactly are those “string” in the if body statements?

For example:

if (num  <  5) {
  "Tiny";
} 

What does “Tiny” represent here? Is it a value to return? Do you want to assign it to something? Do you want to do some computation with it?

As is, it’s just a string “floating” around purposeless.
Hope this helps :slight_smile:

hi its a value that i want returned

so i should remove the condition? Cause the the task says to put that condition

Then review how to return values from a function.
I think this is the challenge about it.

Let us know if you still have doubts or problems :slight_smile:

You have to remove only the last condition, because:

  1. The else statement doesnt take condition.
  2. Look at the other conditions, if num is lesser than 5 , else if its lesser than 10, 15, 20.
    Now the last condition is if the num is greater or equal to the 20. This is evaluated in the last and happens if the other conditions do not work.
    So, you dont need the last condition(its not compulsory).
1 Like

ok so what do i put there, else if?

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";
}


  // Only change code above this line
}

testSize(0);

heres the new script i put and it still wont work :sob:

yes it work when i put "else if " instead of “else”. Thanks for the help :pray:

1 Like

You should see an error that point you to the syntax of your else statement.

I suggest you look again at the if/else syntax

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