This challenge asks us to remove any falsy element from the array.
I’m getting to that but first I want to achieve the following:
Remove any non-boolean elements from the array.
The code below was made to do this but for some weird reason the final console.log(arr) gives the following as output
ate,false
surely ‘‘ate’’ is not of the boolean type hence during this check should be removed too??
The console.log showing every iteration is:
7,ate,,false,9
ate,,false,9
ate,false,9
ate,false
so, any non boolean elements like 7 , ’ ’ (empty string) and 9 are removed but for the life of me I am struggling to see why ‘‘ate’’ is so special such that it is still in the array at the end?
Your code so far
function bouncer(arr) {
for (var row = 0; row<arr.length ; row++) {
console.log(arr);
if ( typeof (arr[row]) !== 'boolean') {
arr.splice(row,1);
}
}
console.log(arr);
return arr;
}
bouncer([7, "ate", "", false, 9]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer