What is wrong in this piece of code?

Q - Remove all falsy values from an array.

Falsy values in JavaScript are false , null , 0 , "" , undefined , and NaN .

Hint: Try converting each value to a Boolean.

  for(let i = 0; i < arr.length; i++){
    if(arr[i] != true){
     arr.splice(i, 1);
  return arr;

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

With the loop you go linear from element 0 to 4. So far so good. But then you remove elements with the splice. The way you call the element to be removed with i doesn’t match up. In other words, because you remove elements the order isn’t the same anymore and you remove wrong elements. (To illustrate this you could enter your code here and step through it: http://pythontutor.com/visualize.html#mode=edit)

You could use the filter() method or push the elements to a helper array and return that …

Problem is as mentioned above by @michaelsndr , and the same code will work if you just change/add this to your code:

if(!arr[i]) { // this will check Flasy values


then add i-- just below your splice method. this will solve the index issue every time it uses splice method.