Returning Boolean Values from Functions : with switch ()?

Returning Boolean Values from Functions : with switch ()?
0

#1

Tell us what’s happening:

Hi, Can I use switch() to replace if-else loop in this case?

Your code so far


function isLess(a, b) {
  // Fix this code
  switch(a,b){
    case:
     a < b;
     return true;
     break;
    default:
     return false;
     break;
  }
}

// Change these values to test
isLess(10, 15);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/returning-boolean-values-from-functions/


#2

A switch statement takes a value or expression between the ( ). You attempted to add two values which is not possible. You could use the value true and then your cases would need to evaluate to true or false depending what you want to return. For what the challenge expects you to do, a Switch statement is overkill. A simple if statement or better yet the use of a ternary operator would be much simpler. Using a ternary operator, this challenge can be solved in a single return statement line. Using an if statement, it can be solved in 3 lines.


#3

Thanks!
I haven’t studied ternary statement yet. Can you give me an example?


#4

You will get to it in the curriculum later. Just focus on an if statement solution for now. You will not need an else statement.