JavaScript Iterators: Mini Linter Project - Step 8 Replace Words


#1

I’m attempting to do replace words in step 8. I’ve gone about it a different way without success, but I’m not understanding why my code is incorrect. Appreciate any help. Here’s my full code: https://gist.github.com/64f57aec93d6462d0c8056b75ded89e8

The specifc snippet is:

betterWords.forEach (word => {
  if (word === overusedWords[0]) {
    let index = betterWords.indexOf(word);
    betterWords[index] === "test";
  }
});

Thanks!
Chris


Mini Linter - Step 8
#2

Please remember to include a link to the project when starting a new topic.


#3

Sorry about that. Since it was part of a different thread, didn’t realize I needed to do that. Here you go!

https://www.codecademy.com/courses/learn-javascript-iterators/projects/mini-linter?course_redirect=introduction-to-javascript


#4

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.

Your code is iterating over an array that does not yet exist. You should be iterating over storyWords using the filter() iterator.


#5

I thought I did that? On line 6 I have:

let betterWords = storyWords.filter(word => ! unnecessaryWords.includes(word));

Maybe that’s not right though.


#6

We cannot see your line 6 but the above looks correct for step 3. Did you find that code in the forum?


#7

I tried to link to my full code. Maybe it didn’t go through. Here it is in full. Sorry for the wall of text.

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(" ");  
let betterWords = storyWords.filter(word => ! unnecessaryWords.includes(word));
let reallyCount = 0;
let veryCount = 0;
let basicallyCount = 0;
let sentenceCount = 0;

//count how many times "really", "very", and "basically" are used
for (let i = 0; i <= betterWords.length; i ++) {
  if (betterWords[i] === overusedWords[0]) {
    reallyCount ++;
  } else if (betterWords[i] === overusedWords[1]) {
    veryCount ++;
  } else if (betterWords[i] === overusedWords[2]) {
    basicallyCount++;
  }
}

//count number of sentences in the paragraph
betterWords.forEach (word => {
  if (word[word.length -1] === (".") || word[word.length -1] === ("!")) {
    sentenceCount ++;
  }
});

betterWords.forEach (word => {
  if (word === overusedWords[0]) {
    let index = betterWords.indexOf(word);
    betterWords[index] === "test";
  }
});

console.log(`Your paragraph includes ${betterWords.length} words.`)
console.log(`You used "really" ${reallyCount} times.`);
console.log(`You used "very" ${veryCount} times.`);
console.log(`You used "basically" ${basicallyCount} times.`);
console.log(`You used ${sentenceCount} sentences.`);

console.log(betterWords.join(" "));

My expected result would be that “really” would be replaced with “test”, but after looking it over, I think forEach() is the wrong method to use, and I should maybe be using map()?

I did step 3 on my own, but after looking at the forum, it looks correct.


#8

Try making that an assignment rather that a comparison.


#9

:man_facepalming:

That was it. Thanks for your help!