I'm studying at freeCodeCamp, and currently learning basic algorithms. I'm doing the falsy bouncer exercise at which you need to remove all falsy values from the array. Sadly there was only the advanced answer for the task, that uses the filter() method. I've decided to make a basic one, but currently stuck.
function bouncer(arr) {
//*loops through array*
for (let i = 0; i < arr.length; i++) {
// *if there is a value that is falsy, delete that value from the array*
if (arr[i] == 0 || arr[i] == NaN || arr[i] == null || arr[i] == false || arr[i] == "" || arr[i] == undefined) {
delete arr[i];
}
}
return arr;
}
console.log(bouncer([7, "ate", "", false, 9]));
It returns:
7,ate,,,9.
The function, indeed removed the falsy values, but I'm left with those three periods(,,,). Is there a way to make this function to work more correct and to return truthy values without those periods, without losing the simplicity of the function? Appreciate your help.