Basic algorith m scripting: Falsy Bouncer

this is mine"

    function bouncer(arr) {
        // Don't show a false ID to this bouncer.
        return arr.filter(Boolean);
    }
1 Like

I came up with two versions:

function bouncer(arr) {
  
  function checkBool(value) {
    return Boolean(value);
  }
  
  return arr.filter(checkBool);
}
bouncer([7, "ate", "", false, 9]);

which can even more concisely be written as

function bouncer(arr) {
  
  function checkBool(value) {
    return !!value;
  }
  
  return arr.filter(checkBool);
}
bouncer([7, "ate", "", false, 9]);

I am slightly confused, wouldn’t this just return the original array? The exercise only wants us to return numbers and strings greater than 0, so if filter is returning boolean values (that evaluate to true), then how does that complete the problem? In other words, how does it know what to filter out?

This is what my “manual” approach looks like

function bouncer(arr) {
  var some = arr.filter(function(x){
    if (typeof x === "string" || typeof x === "number"){
      if (x.length > 0){
        return x;
      }
    } return x;
  });
  return some;
}

what does the double !! do?

You could loose the variable.

My code:

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr.filter(Boolean);
}

bouncer([7, "ate", "", false, 9]);

I did not think to use switch, but wish I would’ve tried it just to get the practice. I haven’t used switch very often.

seems way too simple but it works, question is how does it work any insight ? I read the documentation but they say you need to define another function to perform the tests on the data.