Grammar Checker Walkthrough

Hi community! I’ve been doing the project: Grammar Checker but for some reason in line 33 return word.length < 10; the console said undefined is not an object and I am following exactly what is in the video.
Can someone help me? I’ve been stuck trying to know what’s the mistake :frowning:

Here is my code:

let story =

‘Last weekend, I took literally the most beautifull bike ride of my life. The route is called “The 9W to Nyack” and it 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 literally took me an entire day. I stopped at Riverbank State Park to take some artsy photos. It was a short stop, though, because I had a freaking long way 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 a breathtaking 4,760 feet long! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautifull park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you literally cross back into New York! At this point, you are very close to the end.’;

let storyWords = story.split(" ");

let unnecessaryWord = “literally”;

let misspelledWord = “beautifull”;

let badWord = “freaking”;

let count = 0;

storyWords.forEach((word) => {

count++;

});

console.log(count);

storyWords = storyWords.filter((word) => {

if (word !== unnecessaryWord) {

return word;

}

});

storyWords = storyWords.map((word) => {

if (word !== misspelledWord) {

return word;

}

});

let badWordIndex = storyWords.findIndex((word) => {

return word === badWord;

});

storyWords[78] = “really”;

console.log(badWordIndex);

let lengthCheck = storyWords.every((word) => {

return word.length > 10;

});

console.log(lenghtCheck);

storyWords.forEach((word) => {

word.length > 10 && console.log(word);

})

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

Make sure to pay attention to the error code. As you noted, it indicates that the error is on line 33.

On line 33, you have a typo: lenghtCheck instead of lengthCheck

2 Likes

Thank you! I knew was something with a typo but I couldn’t see it, but still doesn’t work now it said Cannot read property ‘length’ of undefined: word.length > 10 && console.log(word); for line 38 :frowning:

let story =
‘Last weekend, I took literally the most beautifull bike ride of my life. The route is called “The 9W to Nyack” and it 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 literally took me an entire day. I stopped at Riverbank State Park to take some artsy photos. It was a short stop, though, because I had a freaking long way 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 a breathtaking 4,760 feet long! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautifull park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you literally cross back into New York! At this point, you are very close to the end.’;

let storyWords = story.split(" ");
let unnecessaryWord = “literally”;
let misspelledWord = “beautifull”;
let badWord = “freaking”;
let count = 0;
storyWords.forEach((word) => {
count++;
});
console.log(count);

storyWords = storyWords.filter((word) => {
if (word !== unnecessaryWord) {
return word;
}
});

storyWords = storyWords.map((word) => {
if (word !== misspelledWord) {
return word;
}
});

let badWordIndex = storyWords.findIndex((word) => {
return word === badWord;
});
storyWords[78] = “really”;
console.log(badWordIndex);

let lengthCheck = storyWords.every((word) => {
return word.length > 10;
});
console.log(lengthCheck);

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

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

Sorry, but what? What is the purpose of the code you shared? If the word isn’t equal to mispelledWord return it, otherwise, return it anyway. What is the point?
You could simplify it by writing:

storyWords = storyWords;

This is also pointless, but more concise.

1 Like

So, the issue with the undefined value in the storyWords array occurred because the map function was returning undefined for the misspelled word and then the filter function was removing it. To fix this, you can modify the map function to return the misspelled word unchanged when it is encountered, instead of returning undefined . Here’s the updated code:

storyWords = storyWords.map((word) => {
  if (word === misspelledWord) {
    return misspelledWord;
  } else {
    return word;
  }
});

Now, when the filter function removes a word, it won’t create an undefined value in the array. The code should now run without errors.

I hope that makes more sense now!

Umm. It’s still the same code you posted before. If the word is the misspelled word, return it, otherwise, return it. It accomplishes nothing. The point of this part of the project is to replace the misspelled word with the correct spelling. The exercise itself is a bit silly, but it is a learning tool.

Here’s an example of what the exercise is expecting.

Code
const misspelledWord = "freind"; // not from the project - just an example
storyWords = storyWords.map(word => word === misspelledWord ? "friend" : word);
2 Likes

you could try something like this:

const misspelledWord = "freind"; // not from the project - just an example
storyWords.forEach((word, index) => {
  if (word === misspelledWord) {
    storyWords[index] = "friend";
  }
});

1 Like

That works as well, but the project asks the learner to use .map(), and the hint for this step suggests using a ternary expression.

so like this?

const misspelledWord = "freind"; // not from the project - just an example
const correctWord = "friend";
const correctedWords = storyWords.map(word => word === misspelledWord ? correctWord : word);

Sort of. You’re supposed to reassign storyWords to the array returned by .map().

1 Like

Thank you, this work :smiley: I am still learning so I appreciate all the answers! you legends!