Chaining If Else Statements. Can someone explain our give me another source so I can learn this?

Tell us what’s happening:

I am having problems understanding the lesson because, I am finding the lesson too difficult. Can someone give me a guide our a few tips how I can understand this? I already triied going back the previouse lessons for better understanding and w3schools but this one keeps being to difficult for me.

Your code so far


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

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.1 Safari/605.1.15.

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

Check out this link https://www.w3schools.com/js/js_if_else.asp, hope it helps. You can find more information about this topic by googling “JavaScript conditional statements”

Apologies for not trying to explain it myself, but I think the resource above explains it far better than I can.

edit: What is it exactly that you don’t understand?

1 Like

I started to try and explain but ending up coming to the same conclusion. Just a side note though, your return statements aren’t quite right.

//yours
return = ("Huge")
//the one provided by fcc
return "Change Me";
1 Like

Chain the if else statements by first providing if then else if statements lastly provide the else statement. The chain of if else statements should be in a logical order.
So first we check if the num is less than 5, if its true then we return “Tiny”. If the num is not less than 5, we’re going to put several else if statements, check them and if its true we’ll return the respective answers accordingly. Lastly we’ll put our else statement if nothing above match our conditions!

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

Well before this lesson the code was more easy and in w3schools as well
the give an example of

if (time < 10) {
  greeting = "Good morning";
}

and then the exact same code but then with if else but here it becomes like

else if ( *condition3* ) {
*statement3*
. . .
}

What does the * symbole even mean ? and do i need the double … as well? do i need to use the * just as in the example? where did the = go?
Then further upon the lesson they keep changing things
what is a case? where does the ’ came from? we alway’s used " before :frowning: and why is there a ; after a break i thought we could use them without. The : also such a thing.

 case 1:
    answer = 'a';
    break;

They are using * to show they are using pseudo-code, this isnt js. Basically replace *condition3* with some other condition (a boolean), and replace *statement3* with a chunk of code that you want run if condition3 is true, and as long as no previous if or else if 's in that chain have been triggered.

cases are specific to switch statements. the colon : is just part of the switch statements syntax.

' and " are interchangable.

2 Likes

Thank you <3 you are awesome