Grammar Checker question

Hiya! Question about the Grammar Checker walkthrough, which is part of the Iterators module of the Learn JavaScript course. In the video, for step 11, Nate uses .forEach to identify the word longer than 10 characters and logs it to the console, then manually changes that one word in the original block. I am trying to figure out whether it’d be possible to automatically change it, but I can’t seem to figure out how.

Nate’s original code was

storyWords.forEach((word) => {
  word.length > 10 && console.log(word);
})

which printed “breathtaking” to the console.

I thought maybe I could store the output as a new variable and then use .filter to change that one word, but when I log the value of that to the console, it is undefined. Here is my attempt at this (no judgement please, I am quite new to this :laughing:):

let longestWord = storyWords.forEach((word) => {
  return word.length > 10 && console.log(word);
  }
  )
console.log(longestWord);

Any ideas on what I’m doing wrong or maybe how else to accomplish this?

Those two things do not go together in any circumstance. console.log() is always going to be undefined, so falsy.

3 Likes

I’d do this by adding index as an additional parameter, within the forEach loop using an if statement to check if the length is greater than 10, and if so using storyWords[index] to replace it. I’m sure there are other ways too.