Help with Iterators Section Lesson 4 the .Filter Method

Call the .filter() method on randomNumbers to return values that are less than 250 . Save them to a new array called smallNumbers , declared with const .

I’m stuck on the Part that reads, Save them to a new Array, Small Numbers do you just write return then smallNumbers on the end of your statement?

the Question is: Call the .filter() method on randomNumbers to return values that are less than 250 . Save them to a new array called smallNumbers , declared with const .

This is my version, but my two problems are, I can’t call the Function without a Syntax Error… and even if I did call the Function and manage to filter the numbers, how do I get them to go into a new array called small numbers again?

const randomNumbers = [375, 200, 3.14, 7, 13, 852];

// Call .filter() on randomNumbers below
const filteredNumbs = numbers.filter(randomNumbers(number)
{
  return number < 250;
})

here:

randomNumbers(number)

how is this a function? I don’t see a function keyword or an arrow function (=>)

your new array is currently named filteredNumbs, if you want the variable name to be a different, you could change it?

Yes, but I copied out them exactly as shown in the example… i just can’t get this statement to to what its supposed too. I realise now… that const filteredNumbs is a Variable…
In the example, they have written the word Function, so I was just looking around for a Function to replace that with but, "It’s there to declare a Function? Updated , but
still stuck on Syntax Error!

const randomNumbers = [375, 200, 3.14, 7, 13, 852];

// Call .filter() on randomNumbers below
const filteredNumbs = numbers.filter(function (number){
  if(number < 250){
  return number < 250;
  }
})

Check the name of the variable which contains the list of numbers. Is it called numbers?

apart from strings you might need, copy pasting is a terrible idea in my opinion

the examples are deliberately different to discourage that kind of behavior. You learn much more from puzzling it together yourself

Yeah Sorry, having just spent the last 90 minutes writing and rewriting this function, i forget that people only want to make you feel bad, for not being born a coding genius. Don’t worry I’ll post again untill I can find someone who can explain to me the part of the function

I changed where it says numbers to Random Numbers… Still getting the Syntax Message?

const randomNumbers = [375, 200, 3.14, 7, 13, 852];

const smallNumbers []
// Call .filter() on randomNumbers below
const filterNumbs = randomNumbers.filter(function(randomNumber){
  if(number < 250 ) {
    return true;
  }
})

But rewriting from the example will only increase the change of mistakes. Which is why I recommended to write the code yourself. No one is born a programming genius (okay, maybe couple of people, but not many)

your syntax error is here:

const smallNumbers []

you forgot to use the assignment operator (=).

But why would you want to declare/assign smallNumbers here?

Hi thanks for getting back to me yesterday. I think what I couldn’t grasp was that the method .filter requires a variable. And that that Variable produces the Result of the Filter to the Print in the Console. I think thats the theory i’ve understood today. But, as of today I can’t really identify what, num is? Reading the Code it looks, as if they’ve created a function inside of the .filter mehod. And put that function to work/applied it in the same statement? Am I right about that? Thanks!

const randomNumbers = [375, 200, 3.14, 7, 13, 852];

// Call .filter() on randomNumbers below
const smallNumbers = randomNumbers.filter(num => {
  return num < 250;
})

.filter returns an array, what we do with this array is our choice. We could log this array to the console right away, or store the array in a variable

.filter is higher order function, the purpose of high order function is to eliminate some repetitiveness

.filter takes a function as argument, then for each element in the array, will call this function. This is known as a call-back function

when the callback function is called, the current value of the array is passed argument for your parameter (num) in this case

the tricky thing with higher order function, is the layer of abstractions you need to grasp

there is no shame in consulting external documentation if you need to:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

1 Like

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