-1

I'm trying yo filter the numeric values of an array with this code:

 function getNumerics(toFilter) {
        toFilter = toFilter.filter( element =>  !isNaN(element));
        console.log(toFilter);
      }
      
      var toFilter = [1, 'z', '4', 2, 6];
      getNumerics(toFilter);
      console.log(toFilter);

The console.log inside the function shows a correct result but but the last console.log show the array with all the values but if I pass the array to the function why is not change? in javascript all parameters are passed are reference , aren't it?

a_dv85
  • 85
  • 1
  • 6

1 Answers1

0

Your function should return the filtered Array:

function getNumerics(toFilter) {
  return toFilter.filter( element =>  !isNaN(element));
}
      
var toFilter = [1, 'z', '4', 2, 6];
toFilter = getNumerics(toFilter);
console.log(toFilter);
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
andrii
  • 104
  • 5
  • Please avoid answers like *"Try this:"* or *"This should work:"*. Also I would add a wise suggestion to not mutate the input Array. But to create a new one instead. – Roko C. Buljan Dec 04 '22 at 18:06