Javascript reduce method

Hello! Can someone why I got 1 instead of ["a"] (given noF(["a",false]) )in this function? How should I solve this?
I need to get an array that doesn’t contain any falsy values

function noF (arr) {
      
      const newArr= arr.reduce((acc,el)=>{
        if (el){
          return acc.push(el);
        } 
        return acc;
      }, []);
      return newArr;
    }

Hey @fadime, if you look at Array push docs, you will see that push returns the new length of the array, so when you do this in your code:

return acc.push(el);

You are “changing” from an array, to a number, specifically the length :slight_smile:
Perhaps what you want is returning the array with the new value.

However

I need to get an array that doesn’t contain any falsy values

This can also be solved with the filter method, that to me seems more “suited” for the job :slight_smile:
Hope this helps.

1 Like

Thank you for your advice!! I solved :smiley: