please could you explain me this code?
thanks
Beginner/Intermediate solution:
function bouncer(arr) {
//Only return values that evaluate as true inside the array.
return arr.filter(function(value){
if (value){
return (value);
}
});
}
Hope this helps:
function bouncer(arr) {
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;
}
var filtered = arr.filter(truthy);
//This is filtering only the values that evaluate as true in the given array (following the truthy function give above)
return filtered;
}
//The function is now run with a given array, returning only the values that evaluate as true
bouncer([7, "ate", "", false, 9]);
function bouncer(arr) {
// Don't show a false ID to this bouncer.
return arr.filter(function(val) {
return !!val ;
});
}
Super basic solution using forEach and push. Checks all values for true and pushes true ones into a new array.
function bouncer(arr) {
var a = [];
arr.forEach(function(el){
if(el){
a.push(el);}});
return a;}
I managed it with this
function bouncer(arr) {
var newArr= [];
for (var i= 0; i < arr.length; i++){
if (arr[i])
newArr.push(arr[i]);
}
return newArr;
}
I was far, very far from the easy solution given in this topic
there’s no solution, there’s solutionS
Myself – Just now
I came very close…
function bouncer(arr) {
arr = arr.filter(Boolean);
return arr;
}
Damn… could have condensed it down a line. Programmers want to get back to our Geekdom pursuits. Less lines = more time gaming
lol I used a bit different approach, here is the code:
function badValues(val){
return val !== false && val !== null && val !== 0 && val !== "" && val !== undefined && !Number.isNaN(val);
}
function bouncer(arr) {
return arr.filter(badValues);
}
bouncer([1, null, NaN, 2, undefined]);
My solution was a little longer, but it made a lot more sense to me and actually didn’t take long at all.
My code is pasted below, but essentially, I made a loop which would go through each element of the array and put it through a boolean operator to see if it was true. If it’s true, then push that element to a new array called noFalsy. Then, it filters the new noFalsy array for any values of null, because the nature of this particular loop means that if you have items in the middle of your array that are false but perhaps one at the end that is true, all of those false values will become null values in the new array.
function bouncer(arr) {
var noFalsy = [];
for (i = 0; i < arr.length; i++) {
var x = Boolean(arr[i]);
if (x === true) {
noFalsy[i] = arr[i];
} else {
}
}
//filters function and removes any items that are null
var noFalsyFinal = noFalsy.filter(function(noFalsy){
return noFalsy !== null;
});
return noFalsyFinal;
}
bouncer([7, "ate", "", false, 9]);
bit different but here is mine
function bouncer(arr) {
// Don’t show a false ID to this bouncer.
var a=true;
var newArr=arr.filter(function(a){
return a;
});
return newArr;
}
bouncer([7, “ate”, “”, false, 9]);
My code looks like this,
function bouncer(arr) {
// Don't show a false ID to this bouncer.
var newArr = arr.filter(function(element){
if(Boolean(element)){
return element;
}
});
return newArr;
}
Boolean(x) means that we take the value and transform it to boolean, as a result get true or false.
Mine was similar but you can cut it down by just returning the filtered array, without needing newArr
function bouncer(arr) {
return arr.filter(function(val){
if (Boolean(val)){
return val;}
});
}
My code is:
function bouncer(arr) {
var answer = arr.filter(function(teste) {
return teste;
});
return answer;
}
But I don’t understand why _function(teste)_and myFunction(teste) doesn’t work.
Hi can you explain this code. How does it evaluate ‘el’ to true?
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?
buddy u have missed a semicolon @ var newArr=[];
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;
});
}