Here's the challenge:
Eliminate all falsy values from a given array.
Falsy values in JavaScript include false, null, 0, "", undefined, and NaN.
Tips: Try converting each value to a Boolean.
Below is my attempt at solving it:
function bouncer(arr) {
let arr2 =[];
let items = arr.map((item)=>{
if(item == false){
arr2.push(item);
}
return arr;
})
return items;
}
console.log(bouncer([7, "ate", "", false, 9]));
I also experimented with another approach:
function bouncer(arr) {
let arr2 =[];
for(let i = 0; i < arr.length; i++){
if(arr[i] == false){
arr2.push(arr[i]);
}
}
return arr;
}
console.log(bouncer([7, "ate", "", false, 9]));
I thought that at least one of these methods would remove the falsy item from the original array. Why does the mutation not occur in either instance?