freeCodeCamp Challenge Guide: Introducing Else Statements

freeCodeCamp Challenge Guide: Introducing Else Statements
0

#1

When a condition for an if statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With an else statement, an alternate block of code can be executed.

if (num > 10) {
  return "Bigger than 10";
} else {
  return "10 or Less";
}

image


#2

solution:

function testElse(val) {
var result = “”;
// Only change code below this line

if (val > 5) {
result = “Bigger than 5”;
}

else {
result = “5 or Smaller”;
}

// Only change code above this line
return result;
}

// Change this value to test
testElse(4);


#3

When I run the code in the console the result is “5 or smaller”. Yet the “Run Test” function says testElse(5) should return “5 or Smaller” and won’t pass. Must be a :bug:

  var result = "";
  // Only change code below this line
  
  if (val > 5) {
    result = "Bigger than 5";
  } 
  
  else {
    result = "5 or smaller";
  }
  
  // Only change code above this line
  return result;
}

// Change this value to test
testElse(5);