Challenge - Comparison with the Less Than Or Equal To Operator

OPERATORS are killing me softly … what’s going on here:

function testLessOrEqual(val) {
  if (val <= 12) {  // Change this line
    return "Smaller Than or Equal to 12";
  if (val <= 24) {  // Change this line
    return "Smaller Than or Equal to 24";

  return "25 or More";

// Change this value to test

when I entered “a” it returns; “25 or more” … isn’t “a” considered a STRING? How is JS converting this string to a NUMBER?

You are getting the result a as both of the two if statements will fail, resulting in the program continuing to the next line of code
return "25 or more"
In your current code anything other then a number less than 25 will return this result (an array, an object, a string etc.)


Ah, thanks for that … I think some sort of error statement would be better, it seems there’s no error check for the user data …

Yes, I noticed on a lot of the challenges there is no error checking expected for invalid data entry.

That said there is nothing stopping you from for example adding an if statement at the start of the function to verify that the the val supplied is a number and returning an appropriate value (-1, nothing, NaN or null) if that check is failed. If you were implementing such a function in a product I would suggest that input validation would be expected.

1 Like