1

This is for a course I'm taking on Codecademy and I'm stuck. I really want to get a good understanding of JS

The task I'm on is: "There is an array of words that are unnecessary. Iterate over your array to filter out these words. Save the remaining words in an array called betterWords. There are several ways that you could achieve this."

So far, this is what I have, but I'm stuck trying to figure out how to return the "necessary" words to the "betterWords" array:

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

let overusedWords = ['really', 'very', 'basically'];
    
let unnecessaryWords = ['extremely', 'literally', 'actually' ];
    
let storyWords = story.split(' ');
console.log(storyWords.length);
    
storyWords.filter(function() {
  let betterWords = [];
  for (wordsIndex= 0; wordsIndex < storyWords.length; wordsIndex++) {
    for (unnecessaryWordsIndex = 0; unnecessaryWordsIndex < unnecessaryWords.length; unnecessaryWordsIndex++) {
      if (wordsIndex != unnecessaryWordsIndex) {
                    
      }
    }
  }
});

Would I use .splice() to remove the items, then use .map()?

I'm aware I'm probably making this way more complicated than it should be, as that's what I do! Any suggestions and explanations would be wonderful and thank you in advance!

1
  • "Iterate over your array" - is this the array story.split(" ")? What is overusedWords for? Commented Aug 10, 2018 at 17:32

4 Answers 4

1

You are confusing the purpose of the filter function. You should be doing something like:

let betterWords = storyWords.filter(function(word) {
    if (overusedWords.includes(word)) return false;
    if (unnecessaryWords.includes(word)) return false;
    return true;
});

filter does not change the original array.

Sign up to request clarification or add additional context in comments.

1 Comment

I knew I was making it more complicated than needed. This is exactly what I was looking for, and thank you for the explanation as well! I will mark this as the answer SO lets me!
1

You could filter the words by using the result of Array#filter and use Array#includes to check if the word is not in the unnecessaryWords and not in the overusedWords arrays.

var story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.',
    overusedWords = ['really', 'very', 'basically'],
    unnecessaryWords = ['extremely', 'literally', 'actually' ],
    storyWords = story.split(' '),
    betterWords = storyWords.filter(word =>
        !unnecessaryWords.includes(word) &&
        !overusedWords.includes(word)
    );

document.body.appendChild(document.createTextNode(betterWords.join(' ')));

Comments

0

filter returns a new array so you should be doing something like let betterWords = storyWords.filter(...);

1 Comment

Not an answer. This should go in a comment
0

I don't recommend keeping overusedWords and unnecessaryWords as arrays. Doing lookups in these arrays is O(n)--you have to inspect each element of the array every time you do a lookup. If you convert these arrays to sets, you get O(1) lookups. For such a small input, the performance difference is negligible, but the code is easy and clean to write, and this will scale if you have huge lists of overused/unnecessary words.

Also, I recommend splitting on non-word characters rather than spaces. If you have an input containing the word "literally," (with a comma after it), your current approach will miss this word.

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

let overusedWords = ['really', 'very', 'basically'];
let unnecessaryWords = ['extremely', 'literally', 'actually'];

const toRemove = new Set(overusedWords.concat(unnecessaryWords));
const filtered = story.split(/\W+/).filter(e => !toRemove.has(e.toLowerCase()));

console.log(filtered);

This can also be done with a regex one-liner that ignores case and gives you some potential replacement/cleanup options:

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';
let overusedWords = ['really', 'very', 'basically'];
let unnecessaryWords = ['extremely', 'literally', 'actually'];

const result = story.replace(new RegExp(
  "(" + overusedWords.concat(unnecessaryWords).join("|") + ")[^a-zA-Z]*", "gi"
), "");

console.log(result);

2 Comments

Thank you for the advice! As part of the course, the task was to keep it as an array at this point! In the future, I'm sure I'll learn how to do this too and I'll definitely come back to this post for reference. I appreciate it!
Fair enough, but I'd still split on words at the very least. Your current accepted answer would fail on the following test input string: "literally, I wrote". In most coding challenges, efficiency and edge cases are basically the entire scoop, so I can't put forth a solution that doesn't take advantage of these, especially when the cost in terms of code comprehension is none.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.