Array filter function

Hi all

I am trying to write a function that only returns the numbers in an array.

The error message is telling me that the properties of ‘1’ cannot be read in the test array in the function call, but I don’t know why that is.

Can anyone help please?

"use strict";

function filter_list(l) {
  const result = [];

  let i;
  for (i = 0; i < l.length; i++)
    if (typeof l[i] === "number") {
      result.push.l[i];
    }
  return result;
}
console.log(filter_list(["john", 1, "peter", "dave"]));

i don’t know much but i think push method is used like this:

result.push(l[i]);

try that maybe it will work

1 Like

many thanks for your reply

that does not seem to be working either…

I think you’re right about the brackets, but I must be missing something when it comes to pushing an array item into another array (as opposed to an individual string, number etc…)

Hello!
I tried mohamedelmir’s solution and the filter works after the syntax correction:

It’s worth another attempt. If you get an error, please share the error message.

many thanks

it’s still not working with the brackets…

the error message now is “Uncaught SyntaxError: Unexpected token ‘(’ (at script.js:9:19)”…which makes it seem like the brackets shouldn’t be there…?

Please post your updated code

sorry, it’s actually fine. I had an extra ’ . ’ after ‘push’ which I missed.

thanks for all the help everyone

I had an extra ’ . ’

all working now - thank you

1 Like

I had an extra ’ . ’

all working now - thank you

it should be result.push(l[i]);

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.