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(' ');
console.log(splitStory.length);
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]) {
betterWords.push(word);
}
}
}
}
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.
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.
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?