Advertisement
makispaiktis

6. Codecademy Intro - Grammar Checker

Oct 19th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 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.';
  2. let storyWords = story.split(' ');
  3.  
  4. // 1. For Each: Count words
  5. let count = 0;
  6. storyWords.forEach(word => count += 1);
  7. let count2 = storyWords.reduce(((acc, curr) => acc += 1), 0);
  8. console.log(storyWords.length, count, count2, '\n');
  9.  
  10. // 2. Filter: 'Delete' a word
  11. let unnecessaryWord = 'literally';
  12. storyWords = storyWords.filter(word => word != unnecessaryWord);
  13.  
  14. // 3. Map: Replace words
  15. let misspelledWord = 'beautifull';
  16. storyWords = storyWords.map(word => word === misspelledWord ? 'beautiful' : word);
  17.  
  18. // indexOf: replace words
  19. let badWord = 'freaking';
  20. let badWordIndex = storyWords.indexOf(badWord);
  21. storyWords[badWordIndex] = 'really';
  22. console.log(`Bad word index = ${badWordIndex}\n`);
  23.  
  24. // 5. Length Check
  25. lengthCheck = storyWords.every(word => word.length <= 10);
  26. console.log('All words under 11 letters:', lengthCheck, '\n');
  27.  
  28. // 6. Find Index: replace words
  29. index = storyWords.findIndex(word => word.length > 10);
  30. prev = storyWords[index];
  31. storyWords[index] = 'stunning';
  32. console.log(`Word '${prev}' in index ${index} has a length of ${prev.length} > 10 letters, so:\nIt is replaced with the word 'stunning'\n`);
  33.  
  34. console.log(storyWords.join(' '));
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement