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]);
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]);
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.
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?
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.
Here my btw, very close to authors.
My code very close looks to peach and sweet potato
if no difference in speed issue between all campers code i choose ChadKreutzer code,
really sexy code and short looking like .
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]);
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]
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 ?
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 )
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);
}
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 ?
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.
I used Array.prototype.filter() in my code. Hope this helps
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;
}
}