Introducing Else Statements

Introducing Else Statements
0

#1

Hi there, so the assignment is to combine two if statements into one if/else statement. I have done so, it works on Chrome, I submitted in the help query to see if I was doing it wrong, but they said it was correct, so I am reporting it as a bug.

Here is my code:

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

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

Here’s the error I’m getting: You should only have one if statement in the editor

I also tried to remove the else if, and just do else like this: (but got the same error):

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);

thanks!
Crystina


#2

HI Crystina

Your first else if statement works fine. In this situation i would try F5 for a refresh or copy your code close the browser open it again and paste your code in the FCC challenge. Most times it works then.

Here is the evidence that your code works, tested with FF on JS Tester

Regards.
Phajava


#3

Awesome thank you so much! I had tested it too and it worked so i was confused. Thanks so much for your help!!

Crystina


#4

Liking the JS Tester.

Thanks :grinning:


#5

If it is a function to return 1 value you may not need an else statement because return acts as break in other words any code after a return being called is not executed.

function testWithoutElse(val) {
  if (val > 5) {
    return  "Bigger than 5";
  }
  return "5 or Smaller";
}

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

Though the purpose of the challenge is to make sure you get the if command flow

  if (cond1) {
    // statement
  } else if (cond2) {
    // statement2
  } else {
    // not cond1 and not cond2
  }

#6

The correct answer is:

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

if (val > 5) {
return “Bigger than 5”;

} else {
return “5 or Smaller”;
}

// Only change code above this line

return result;

}

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