Regarding Iterators

Hey guys,

Just a quick question regarding iterators. So I’ve gone through the exercise (Iterators, part 4 with .filter())

Using the below code, why do I receive an error? I’m trying to get used to using arrow notation, and from what I can tell, it should be correct. Where am I going wrong?

Thanks a ton!

Giordano

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

// Call .filter() on randomNumbers below

const smallNumbers = randomNumbers.filter(x => x < 250);

const favoriteWords = ['nostalgia', 'hyperbole', 'fervent', 'esoteric', 'serene'];


// Call .filter() on favoriteWords below
const longFavoriteWords = favoriteWords.filter (y => favoriteWords.length > 7);

If we actually log the output:

// Call .filter() on favoriteWords below
const longFavoriteWords = favoriteWords.filter (y => favoriteWords.length > 7);
console.log(longFavoriteWords);

we can see that longFavoriteWords is empty

which very likely means you made a mistake in the comparison of your filter function.

You are checking if the array contains more then 7 elements, while you should check if each word has more then 7 characters.

1 Like

Try looking at the parameter of the fat arrow function passed into the .filter() method.

const longFavoriteWords = favoriteWords.filter (`y` => `favoriteWords`.length > 7);

Are you sure you want to compare the size of the entire array? Or, Do you only want to compare each word?
To understand what favoriteWords.length is returning, try logging it in your console using:

console.log(`${favoriteWords.length}`);

And try and understand
favoriteWords.length > 7 takes the length of the entire array of words and compares it with 7. Do you want that?

Try fixing that by using the parameter of the fat-arrow function and you’re good to go!

I deliberately didn’t include certain things in my answer, much more insight can be gained by learners when they reach certain conclusions themselves. I merely try to nudge them in the right direction.

1 Like

I got it!

Thanks for the hints everyone!

1 Like

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