# Basic Algorithm Scripting - Falsy Bouncer

### **Tell us what’s happening: **

Hi when I test the! then a Boolean it returns true and seems to return false for other things. Why does this code not preform it’s intended purpose?

### Your code so far

``````function bouncer(arr) {
let count = 0;
let joe = arr.slice(0,arr.length);
for(let i = 0;i<arr.length;i++){
if(!joe[i]){
joe. splice(i,1);
}
return joe;

}

}

console.log(bouncer([7, "ate", "", false, 9]))
;
``````

### Your browser information:

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.69`

### Challenge Information:

Basic Algorithm Scripting - Falsy Bouncer

You can add printing to console to see what is happening within the loop. For example add this just below the line with `for`:

``````console.log(i, joe);
``````

The problem with using `splice`, especially inside a `for` loop, is that it mutates the array. So, your array indices will change as the loop iterates the array, meaning you don’t get the result you want.
It would likely be easier to solve this challenge using the `filter` method instead.

Also, your `for` loop is not iterating the full array anyway, as your `return` statement is inside the loop, so will `return` out of the function at the end of the first iteration of the loop.

1 Like

Hello Mr.Igorgetmeabrain
I have edited my code and am now using the filter method.
Could you please explain what is running wrong?

``````function bouncer(arr) {
let joe = arr.filter(falsyFind);
return joe
}
function falsyFind(input){
if(!input){
return true
}
return false
}

bouncer([7, "ate", "", false, 9]);
``````

I have updated the code as follows

``````function bouncer(arr) {
let joe = arr.filter(isBoolean);
return joe
}
function isBoolean(input) {
if(input instanceof Boolean){
return true
};
return false
}
console.log(bouncer([7, "ate", "", false, 9]));
``````

Hello esteemed sir
I managed to figure it out.
I had not thought of the built in method Boolean which I could use instead of !input
Thank you very much

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.