Why can't I get this for loop inside a filter iterator to work?


I’m at my wits’ end with the “Mini Linter” project. I have been trying to filter out the words from the “unnecessaryWords” array, and I can’t figure out what I’m doing wrong. Here are a few of the things I’ve tried:

let story = '...';

let overusedWords = ['really', 'very', 'basically'];

let unnecessaryWords = ['extremely', 'literally', 'actually' ];

const splitStory = story.split(' ');


let betterWords = splitStory.filter( word => {
  for (let i = 0; i < splitStory.length; i++) {
    for (let j = 0; j < unnecessaryWords.length; j++) {
      return splitStory !== unnecessaryWords;

console.log(betterWords.join(' '));
let betterWords = [];

const getThemOut = word => {
  for (let splStIndex = 0; splStIndex < splitStory.length; splStIndex++) {
    for (let unWdIndex = 0; unWdIndex < unnecessaryWords.length; unWdIndex++) {
      if (splitStory[splStIndex] !== unnecessaryWords[unWdIndex]) {
const betterWords = splitStory.filter( word = {
  for (let splitStoryIndex = 0; splitStoryIndex < splitStory.length; splitStoryIndex++) {
    for (let unWdIndex = 0; unWdIndex < unnecessaryWords.length; unWdIndex++) {
      if (splitStory[splitStoryIndex] !== unnecessaryWords[unWdIndex]) {
        return word;

These either return an error or return the original array with no changes. I don’t get it. Please help me.


Filter is an iterator so there is no need of a for loop. Perhaps you are overcomplicating things?


So I cannot use for loops inside an iterator? Does this apply to while and do … while loops, as well? Might it work if I used nested for loops only? Thank you for your advice.


Give filter an array context and a conditional functon to apply on each element. A new array will be returned of elements that match the condition.

array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
evens = array.filter(x => x % 2 == 0);
// <- (4) [2, 4, 6, 8]

That’s without any loops in the function. Just a return.

The thing to note is that we are building a array so need an iterative return (when appropriate). If your function needs to loop over each x, where x is an array, then yes, you could have a loop with a return following it. That loop would run once through on each iteration of the filter’s internal loop. However, this is more complex than necessary and could probably be worked out some other more direct way so the filter doesn’t have the overhead of a messy function.

At the moment I’m not all that imaginative when it comes to dreaming up a special case to study. If you run across one, or have an example, do share it in a reply to this topic.


Sorry for the late reply after you went out of your way to help. I was playing with a bunch of code on an online JS compiler, trying to understand your example, and I think I finally have done. So the iterators contain the loops I’m trying to create already, because they are essentially like methods called on an object: the iterators contain that code already. If I want to add another loop, I need to be mindful of the iterator’s internal loop. Is that about right?


Correct. Iterators iterate. map(), filter() and reduce() all behave in similar fashion.


Thank you so much. I will mark your original answer as the answer.


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