Ad
  • Custom User Avatar

    This solution only works due to incomplete testing. It checks whether a is truthy. Here are some possible falsy cases in JavaScript:

    false,
    null,
    0,
    NaN,
    "" (empty string),
    undefined

    So if you have any value other than these, such as a string like "abc" or simply the boolean true, this function wouldn’t work.

    Additionally, even though this is a function expression, which is good practice, you should use the const keyword. Not using it is considered bad practice. And don’t forget the semicolon at the end.

  • Custom User Avatar

    Not entirely true since null or an empty array are falsy. Though when you get a string or a number other than 0, those are truthy. Or what if you have a variable with "true" as its value? Your solution is correct though.

  • Custom User Avatar

    #include <algorithm'> is not necessary here since the function used is part of the string library, not the algorithm library.