I don’t understand why my function isn’t working. I see the function putting each value of the array into Boolean(). Then it uses a ternary operator. If Boolean() returns true nothing happens, if it returns false, the array is spliced that element is removed.
**Your code so far**
function bouncer(arr) {
for (let i of arr) {
Boolean(arr[i]) ? arr : arr.splice(i, 1);
}
return arr;
}
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/103.0.0.0 Safari/537.36
I see, thx! I solved the problem btw! This was my solution. However, when I used a for of loop I would get empty braces instead of the answer. I thought for of iterated through each element of the array.
function bouncer(arr) {
let result = [];
for (let i = 0; i < arr.length; i++) {
if (Boolean(arr[i]) === true) {
result.push(arr[i]);
}
}
return result;
}