Tell us what’s happening:
I am looping through all the array elements and checking for any falsy values usinf if loop, but the function only removes the first falsy value i.e. “” and it does not remove false. Why is it happening? Why is it not removing the second array element?
P.S. : I have used splic function to remove the element from an array.
Your code so far
function bouncer(arr) {
// loop through arrayvalues one by one
for (let i = 0; i < arr.length; i++) {
// console.log('length',arr.length)
if (arr[i] === false || arr[i] === null || arr[i] === null || arr[i] === 0 || arr[i] === "" || arr[i] === undefined || arr[i] === NaN) {
console.log('the array to be removed',arr[i])
console.log('index of the array',i)
arr.splice(i,1)
console.log('array after removing the falsy value',arr)
}
}
console.log('array outside the function',arr)
return arr;
}
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/80.0.3987.149 Safari/537.36
.
Challenge: Falsy Bouncer
Link to the challenge: