freeCodeCamp/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/introducing-else-if-statements/index.md
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

1.3 KiB

title
Introducing Else If statements

Introducing Else If statements


Problem Explanation

We'll be modifying the existing code above so that it follows the flow of logic that an else-if statement has.

Hint 1

   if (val > 10) {
    return "Greater than 10";
  }

All if statements and their variants start off with an if statement.

Hint 2

  else if (val < 5) {
    return "Smaller than 5";
  }

Statements between the if statement and the else statement in an else-if flow are in the else-if format

Hint 3

else {
  return "Between 5 and 10";
  }

The last statement in an else-if flow is in the else format


Solutions

Solution 1 (Click to Show/Hide)
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);

Code Explanation

The structure of an else-if logic flow is an initial if statement, one more if-else statements, and one final else statement.