freeCodeCamp Challenge Guide: Chaining If Else Statements

freeCodeCamp Challenge Guide: Chaining If Else Statements
0

#1

if/else statements can be chained together for complex logic. Here is pseudocode of multiple chained if / else if statements:

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

#2

SPOILER ALERT!!!

Answer

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

#3

Hey, whats wrong with my code? i’ve tested it and it gives expected results but the test doesnt realize it


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";
 
  // Only change code above this line
}

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

#4

capital letter for medium M and huge H


#5

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

Is it really necessary to have 4 else statements ? Because this code works properly ( and saving lines of code ).