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?
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.
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
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.
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, ""]);
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.
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”.
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 '[]'
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]);
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