Advertisement
makispaiktis

Codecademy - 21st Exercise (Iterators in Array - Javascript)

Dec 19th, 2019 (edited)
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. JAVASCRIPT: ARRAYS, LOOPS, AND OBJECTS
  4. Mini Linter
  5. In this project, you will use what you know about iterating over arrays to improve the quality of a paragraph and gather some information about that paragraph.
  6.  
  7. This is the same type of work that word processing software does. Additionally, you may have heard of linting, a process by which text is evaluated and improved by an application. In this project, you will create a miniature version of a linter using array methods that you have learned.
  8.  
  9. If you get stuck during this project or would like to see an experienced developer work through it, click “Get Help“ to see a project walkthrough video.
  10.  
  11. Tasks
  12. 8/8Complete
  13. Mark the tasks as complete by checking them off
  14. 1.
  15. In the code editor, there is a string called story. We want to gather some information about the individual words and sentences in the string. Let’s split the string into individual words and save them in a new array called storyWords.
  16.  
  17. 2.
  18. Log how many words there are in this story to the console.
  19.  
  20. 3.
  21. 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.
  22.  
  23. 4.
  24. There is an array of words called overusedWords. These are words overused in this story. You want to let the user of your program know how many times they have used these overused words. There are two ways to achieve this. Try it on your own first. If you need help, consult the hint.
  25.  
  26. 5.
  27. Now, count how many sentences are in the paragraph.
  28.  
  29. This may seem tricky, but remember that all of the sentences in this paragraph end with a period (.) or an exclamation mark (!). You could iterate over the array and add 1 to a sentence counter variable for each word that has a period or exclamation mark as its final character.
  30.  
  31. 6.
  32. Log these items to the console:
  33.  
  34. The word count
  35. The sentence count
  36. The number of times each overused word appears
  37. You could choose to simply log them one by one or, for a challenge, create a function that logs all of them with some formatting.
  38.  
  39. 7.
  40. Now, log the betterWords array to the console as a single string.
  41.  
  42. 8.
  43. Congratulations! You’ve improved the original paragraph and given the user some important information about his or her work. Think about ways in which you can extend this project, potentially by using other JavaScript knowledge you have.
  44.  
  45. Here are some ideas:
  46.  
  47. For the overused words, remove it every other time it appears.
  48.  
  49. Write a function that finds the word that appears the greatest number of times.
  50.  
  51. Replaced overused words with something else.
  52.  
  53. */
  54.  
  55.  
  56.  
  57.  
  58.  
  59. 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.';
  60.  
  61. let overusedWords = ['really', 'very', 'basically'];
  62.  
  63. let unnecessaryWords = ['extremely', 'literally', 'actually' ];
  64.  
  65. let storyWords = story.split(' ');
  66. console.log(storyWords.join(' '));
  67. console.log();
  68. console.log("Words of story: " + storyWords.length);
  69.  
  70. let betterWords = storyWords.filter(word => {
  71.   return (word != unnecessaryWords[0] && word != unnecessaryWords[1] && word != unnecessaryWords[2]);
  72. });
  73.  
  74. console.log("Better words of story: " + betterWords.length);
  75.  
  76. let overusedArray = storyWords.filter(word => {
  77.   return (word === overusedWords[0] || word === overusedWords[1] || word === overusedWords[2]);
  78. });
  79.  
  80. console.log("Overused words: " + overusedArray.length);
  81.  
  82. let numOfSentences = 0;
  83. for(let i=0; i<story.length; i++){
  84.   if(story[i] === '.' || story[i] === '!'){
  85.     numOfSentences++;
  86.   }
  87. }
  88. console.log("Number of sentences in story string: " + numOfSentences);
  89.  
  90. console.log();
  91. console.log("Better words story: ");
  92. console.log(betterWords.join(' '));
  93. console.log();
  94.  
  95. // Additional Functions
  96. function removeOverused(storyWords){
  97.   // First, I will create a new array with the following attributes:
  98.   // Length = overusedWords.length, because in this array
  99.   // I will match each overused word with a counter of its usingnes// in     // the story
  100.   let newStoryWords = [];
  101.   let counterOverused = overusedWords.map(word => 0);
  102.  
  103.   for(let i=0; i<storyWords.length; i++){
  104.     // Now, I have access to a single word of story
  105.     newStoryWords.push(storyWords[i]);
  106.     for(let j=0; j<overusedWords.length; j++){
  107.       if(storyWords[i] === overusedWords[j]){
  108.         // console.log(overusedWords[j]);
  109.         counterOverused[j]++;
  110.         // console.log(counterOverused[j]);
  111.       }
  112.       // Check if counter > 1, to delete the word
  113.       if(counterOverused[j] > 1){
  114.         // I will remove the word that I last entered in my new array
  115.         newStoryWords.pop();
  116.       }
  117.     }  //   End of 2nd for-loop
  118.   }  // End of 1st for-loop
  119.   return newStoryWords;
  120. }
  121.  
  122. newStoryWords = removeOverused(storyWords);
  123. console.log("New story words: ");
  124. console.log(newStoryWords.join(' '));
  125. console.log();
  126. console.log();
  127.  
  128. // Second function
  129. function findMax(storyWords){
  130.   // First, I will create a new array that contains all the single words
  131.   // of the initial array
  132.   singleWords = [];
  133.   singleWords.push(storyWords[0]);
  134.   for(let i=1; i<storyWords.length; i++){
  135.     let counter = 0;
  136.     for(let j=0; j<singleWords.length; j++){
  137.       if(storyWords[i] != singleWords[j]){
  138.         counter++;
  139.       }
  140.     }
  141.     if(counter === singleWords.length){
  142.       singleWords.push(storyWords[i]);
  143.     }
  144.   }
  145.   // Secondly, I will create a new array, in which I will match
  146.   // all the elements of singleWords with a counter of this
  147.   // word's appearance
  148.   let counterAppearances = singleWords.map(word => 0);
  149.   // The new array has "n" elements, where
  150.   // n = singleWords.length
  151.   // Scanning again the string to increase the appearances
  152.   for(let i=0; i<storyWords.length; i++){
  153.     for(let j=0; j<singleWords.length; j++){
  154.       if(storyWords[i] === singleWords[j]){
  155.         counterAppearances[j]++;
  156.       }
  157.     }  // END OF 1ST FOR-LOOP
  158.   }  // END OF 2ND FOR-LOOP
  159.  
  160.   // Last step is to check the max value of appearances
  161.   let positionOfMax = 0;
  162.   let max = counterAppearances[0];
  163.   for(let i=1; i<counterAppearances.length; i++){
  164.     if(counterAppearances[i] > max){
  165.     max = counterAppearances[i];  
  166.     positionOfMax = i;
  167.     }    
  168.   }
  169.  
  170.   // Now, I have the position of max-appearances element and the max value
  171.   let singleWordMax = singleWords[positionOfMax];
  172.   console.log("Most word-appearances in story: ");
  173.   console.log("'" + singleWordMax + "' ----> " + max + " times");
  174. }
  175.  
  176. findMax(storyWords);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement