Basic algorith m scripting: Falsy Bouncer

Basic algorith m scripting: Falsy Bouncer
0.0 0

#1

hi…
I share my works and need to learn from the way you resolve yours
MINE WORKS, but i didn’t use Boolean Objects nor Array.filter()

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
 function filterArr(value) {
  switch (value){
    case value!=="":
    case value!==0:
    case value!==false:
    case value!==null:
    case !isNaN(value):
    case value!==undefined:
      break;
    default:
      return value;          
  }
}

return arr.filter(filterArr);
}

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

#2

I just went back and rewrote my solution using filter():

function bouncer(arr) {
  var result = arr.filter(function(i) {
    return i;
  });
  return result;
}

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

This was my original solution:

function bouncer(arr) {
  var result = [];
  for (var i = 0; i < arr.length; i++) {
    if (arr[i]) {
        result.push(arr[i]);
        }
  }
  return result;
}

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

#3

I used Boolean

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

Basically Boolean is false items, or all the items we need to filter out for this task. so the .filter is filtering out the false items and returning the rest to the filtered variable.


#4

Mine works…but trying to find out why:

function bouncer(arr) {
  for (var i=0; i<arr.length; i++) {
    if (Boolean(arr[i]) === false) {
      arr.splice(i,1);
      **i--;**
    }
  }
  return arr;
}

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

Before, without “i–”, it didn’t work but once I added it…it worked. I understand why it doesn’t work without it, but can anyone explain to me what “i–” does?


#5

Because .splice():

changes the content of an array by removing existing elements and/or adding new elements.

After if loop executes your array is one position shorter and i in for loop after increment is pointing to the next+1 element of the array.

When you do i-- you set i one step back so it points to the correct element.

Hint:
Always check if method changes the original array.


#6

super short version just using filter:

function bouncer(arr) {
  return arr.filter(item => item);
}

#7

Hi @ChadKreutzer,

What is item => item doing here?

Thanks for any insight.


#8

I solved it with:

function isFalsy (val) {
  if (val) { return val; }
}

function bouncer(arr) {
  return arr.filter(isFalsy);
}

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

#9

Sorry it took so long for me to get back to you. That is a shorthand way to say:

function(item){ return item; }

so if I didn’t use the arrow notation the function would look like this:

function bouncer(arr) {
     return arr.filter(function(item){
          return item;
     });
}

This takes advantage of the fact that filter will only add an item to the filtered array if it returns true.


#10

Thanks for the excellent explanation. Very helpful!


#11

Here is my solution,
> function bouncer(arr) {
> function nonFalsy (arr) {
> if (arr != 10) {
> return arr;
> }
> }
> return arr.filter(nonFalsy);
> }
> bouncer([7, “ate”, “”, false, 9]);


#12

Thanks for that explanation. I stumbled onto this solution by accident and was trying to find out why it worked, apparently I missed that part of the docs when I was reading them.


#13

here is my solution. It works but do not know how :slight_smile:

function bouncer(arr) {
return arr.map(function(val){return val;}).filter(function(val){
if(val!==false && val!=="" && val!==0 && val!==null && val!==undefined && isNaN(NaN))
return val;
else{
return;
}});
}


#14

Here my btw, very close to authors.
My code very close looks to peach and sweet potato :peach::sweet_potato:
if no difference in speed issue between all campers code i choose ChadKreutzer code,
really sexy code and short looking like :candy:.

function bouncer(arr) {
//EvilEpicCoder
var nArr=[""];
nArr=arr.filter(function(a){
switch (a){
case (false):
case (null):
case (0):
case (""):
case (undefined):
case (NaN):break;
default:
return a;
}
});
return nArr;
}
bouncer([7, “ate”, “”, false, 9]);


#15

Hey Im wandering how your function work, when you give a filter as Boolean, so filtered should return the opposite so an array with boolean only…
Check my example below, here I give a filter of value >= 10 :


function isBigEnough(value) {
return value >= 10;
}

var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]


#16

Hey jenovs, could you explain both code above… First I’ve started same as you with a loop, so :


function bouncer(arr) {
  var result = [];
  for (var i = 0; i < arr.length; i++) {
    if (arr[i]) {
        result.push(arr[i]);

// 1st loop : arr[0] = 7 so pushing to result
// 2nd loop : arr[1] = “ate” so pushing to result
// 3rd loop : arr[2] = “” so also pushing result…
// 4th loop : arr[3] = false so also pushing…
// 5th loop : arr[4] = 9 so pushing to result
… every loop are push… or I guess I should miss something ?

        }
  }
  return result;
}

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

#17

No, every loop is not a push. You have an if statement before your push and this if statement checks if arr[i] is not Falsy (hence the name of the challenge :wink: )

The code with filter() goes through every element in an arr and if that element (i) is truthy it returns it, if not it gets discarded.

First version could be alse written like this:

function bouncer(arr) {
  return arr.filter(Boolean);
}

#18

ok so that mean when i is equal to a falsy is discarded, but I though [i] means only the index so when : if (arr[2)) is truthy cuz its should return “” same as arr[3] should return “false” … thats why i dont understand the arr[i] it means… if there is something in arr[i] ? and something means a number or a string or any other true value ?


#19

The input of the function (arr) is an array, so [i] means the element of the input array.

arr[2] is empty string so if (arr[2]) evaluates to false.
arr[3] is false (it is a value false, it is not a String, no quotation marks) so it obviously evaluates to false.


#20

I used Array.prototype.filter() in my code. Hope this helps :sunglasses:


function bouncer(arr) {
  // Send each element of 'arr' to falsy function
  return arr.filter(falsy);
}

function falsy(value) {
  /* Compare each element with falsy values. 
     (Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.)
     If element does not match falsy input, then return the element. 
     This will exclude all the falsy values from an array (if any) from being returned.*/
  
  if (value !== (false || null || 0 || "" || undefined || NaN)) {
    return value;
  }  
}