freeCodeCamp Challenge Guide: Falsy Bouncer

hey @HebleV. so in my code where i say

if(el){
 a.push(el);}});

since it is in a forEach loop it checks every element in the array. if(el) is the same as saying if(el===true) and if(!el) is the same as if(el===false)
and if el is true it gets pushed into new array called a.
does that help?

3 Likes

buddy u have missed a semicolon @ var newArr=[];

1 Like

function truthy(value) {
//only return the given value if it evaluates as true.
//For example, 2, “string”, true, will all return, but false, NaN, -1 will not.

  return value;

}

Why would it always return true??
Can please anyone explain.

my code:

function bouncer(arr) {
  return arr.filter(function(el) {
    return Boolean(el) !== false;
  });
}

2 Likes

What does “!!” mean/do?

A lesson in the importance of sleep:

function bouncer(arr) { //filter by existance arr =arr.filter(function(Val){ //below is a small monstrosity that proves trouble sleeping can // be detrimental to your code if(Val === undefined ||Number.isNaN(Val)){ Val -= Val; // undefined - undefined == null; Val -= null; //undefined and NaN turns to null, null - null == 0; return Val == 0; //just in case null - null stops being == 0; } return Val != null && (Val > 0 || Val.length > 0); //not null && greater than //or bigger than 0

});
return arr;
}

Wow, that was really easy with .filter(). Nonetheless, I wrote a method without using .filter(), only a loop:

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

I now realise that I could have left out (Boolean(arr[i]) == true), as arr[i] already returns a truth value.

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


bouncer([false, null, 0, NaN, undefined, ""]);
1 Like

here is my solution.

function bouncer(arr) {
  return arr.filter(function(s){
    if(s)
    return s;
  });
}
bouncer([1, null, NaN, 2, undefined]);

I had to look up Boolean objects which was quite confusing with all the things you can do with it compared to a Boolean primitive

Anyways, these are my solutions. I was practicing ECMA6 scripting and various formatting. Below all solutions work

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

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

Function call inside filter

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var newArr=arr.filter(function(val){
    return Boolean(val);
});
  return newArr;
}
bouncer([7, "ate", "", false, 9]);

One liner

javascript
function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr.filter(function(val){return Boolean(val);});
}
bouncer([7, "ate", "", false, 9]);

ECMA 6 scripting

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
 return arr.filter(val => Boolean(val));
}
bouncer([7, "ate", "", false, 9]);

In hindsight I could’ve just written the advanced code solution from the start.

3 Likes

function bouncer(arr) {
// Don’t show a false ID to this bouncer.
return arr.filter(function(ar){
if(Boolean(ar) !== false){
return ar;
}
});
}

I feel I am dumb

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

You don’t need the double negation here - since most objects evaluate as truthy values, just return the callback argument. !!val and val are logically equivalent.

In logic, it’s called a double negation. Most commonly used when working through logical proofs to keep explicit logical rules such as modus ponens, etc.

!!val is logically equivalent to val - you are in essence saying “not the case that not-val” which means “val”.

1 Like

Since all values evaluate as truthy in JavaScript (with the exception of the falsy ones supplied in the example), we can simply return the callback argument and then return the new array, too.

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var newArr = arr.filter(function(el) {
    return el;    // returns all truthy values
  });
  return newArr;
}

bouncer([false, null, 0, NaN, undefined, ""]);    // returns '[]'

This is how I did it:

function bouncer(arr) {
// Don’t show a false ID to this bouncer.

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

bouncer([7, “ate”, “”, false, 9]);

Its a long way but, i need to check for NaN, Help Here.

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var newArr = arr.filter(function(word){
    return word != false && word != 0 && word != undefined && word != "" && word != null ;
  });
  
  return newArr;
}

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

This was my solution until i saw the advanced solution on StackOverflow but i made sure i came up with it on my own so here it is

function bouncer(arr) {
  var notFalse;
  var isFalse=[false,"",null,undefined,NaN,0];

  notFalse=arr.filter(function(val){
    if(val!=isFalse){
      return val;
    }

  });
  return notFalse;
 }

 bouncer([0, NaN, "", false, undefined,null]);

Very bad solution but it works !

var tempBoolean;
var tempElement;
var trueArray =[];
var testArray = [];
var concatedTrueArray =[];
for(var a=0; a<arr.length; a++){
tempBoolean = Boolean(arr[a]);
testArray.push(tempBoolean);
if(tempBoolean==true){
tempElement=arr.slice(a,(a+1));
trueArray.push(tempElement);
}
}
for(var b = 0; b < trueArray.length; b++){
for(var c= 0; c < trueArray[b].length; c++){
concatedTrueArray.push(trueArray[b][c]);
}
}
return concatedTrueArray;

Another less advanced solution:

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  function clean(element){
    if(element !== undefined || null || 0 || "" || NaN || false ){
      return element;
    }
  }

  return arr.filter(clean);
}

Never thought to just stick Boolean in the filter facepalm

3 Likes