Mini linter Contribution

Hi All,
I’ve completed the Mini-Linter project on Full Stack Engineering Course.
https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-iterators/projects/mini-linter

It’s kind different from the official video. So I decide to share.
Please, comment, give your opinion about what I can improve, about good (or bad) practice or any other point.

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' ]; // 1. let storyWords = story.split(' '); // 2. console.log ('There are ' + storyWords.length + ' words in the story\n'); // 3. let betterWords = storyWords.filter(word => !unnecessaryWords.includes(word)); // 4. // It counts overused words and returns an array with results. // First, It creates the function overusedCounter and declares 2 variables: // 1. report object: stores the data for returning. // 2. overusedOccurencies: stores an array with all occurrences of each overused word in storyWords. // It loops overusedOccurrences and update report object. // If the property already exists in report, for the current word in overusedOccurrences, // it increments the property that already exists. // if not, it creates the property defining its initial value to 1. let overusedCounter = function() { let report = {}; let overusedOccurrences = storyWords.filter(word => overusedWords.includes(word)); overusedOccurrences.forEach(word => { if (report.hasOwnProperty(word)) { report[word] ++ } else { report[word] = 1; } } ); return Object.entries(report); } /* another version also works out. const overusedCounter = () => { let occurrences = []; let arrReturn = []; for (i = 0; i < overusedWords.length; i++) { occurrences.push(0); } for (i = 0; i < overusedWords.length; i++) { for (j = 0; j < storyWords.length; j++) { if (overusedWords[i] === storyWords[j]) { occurrences[i]++ } } } for (i = 0; i < overusedWords.length; i++) { arrReturn.push([overusedWords[i],occurrences[i]]); } return arrReturn; } */ // 5. // For counting sentences, It first filters the storyWords array. // The creteria is .includes() punctuation characters as arguments. // At last, It returns the wordsPunctuated array with .length property // meaning the numbers of sentences. let countSentences = function () { let sumWordsPunctuated = storyWords.filter(word => word.includes('.') || word.includes('!') ) return sumWordsPunctuated.length; } //6. // Accepting the challenge :) , a logInfo() function with formated output. const logInfo = () => { console.log (`\nThe story has: \n ${storyWords.length} words, \n ${countSentences()} sentences and \n the following overused words:`); // It calls overusedCounter() forEach method to show overused words totals. // x[1] is the number of occurrences. // x[0] is the word overused. // i is the array index, given by the .forEach method. // inside forEach, an if statement for treaten plural and singular outputs overusedCounter().forEach ((x,i) => { if (x[1] > 1) { console.log(` ${i+1}. "${x[0]}": ${x[1]} occurrences.`) } else { console.log(` ${i+1}. "${x[0]}": ${x[1]} occurrence.`) } }); } // 7. // logging betterWords console.log(`\n\The Better Words\n\n ${betterWords.join(' ')} \n`); // 8.1 // This function remove overused words. // firstOverusedInStoryIndex array stores the indexes of // the first occurrences of each overused word in storyWords. // This is the elements that will be preserved. // A for loop looks up, in storyWords, the index of the current overused word and // adds it to firstOverusedInStoryIndex. // Then, an output not demanded by the task but I include just for montoring // how function works out. const removeOverusedWords = () => { let firstOverusedInStoryIndex = []; for (let i = 0; i < overusedWords.length; i++) { firstOverusedInStoryIndex.push(storyWords.findIndex(word => word === overusedWords[i])); } console.log ('\n------------------------\nRemove Overused Words\n\nThe indexes of the first occurrences of each overused words are ' + firstOverusedInStoryIndex + '\n'); // A second loop process the overwriting of the elements in storyWords // which are overused words, except the first occurrence. // It checks, in storyWords, if the current element is included in overusedWords and // if is not the first occurence. // Then, another output just for monitoring. // So it overwrites the value of the element stored on the index // of the current word, without removing the element, writing an empty // to avoid altering the index of the subsequent overused words occurencies in storyWords. for (let j = 0; j < storyWords.length; j++) { if (overusedWords.includes(storyWords[j]) && !firstOverusedInStoryIndex.includes(j)) { console.log ('"' + storyWords[j] + '" on index ' + j + ' of storyWords is being removed...') storyWords[j]=''; } } // This third loop iterates on storyWords removing empties. // As the search is for the same value, i.e. an empty element, // there is no problem on modifying the subsequent indexes of // the subsequent empties. let indexToRemove = storyWords.findIndex(word => word === ''); for (let i = 0;indexToRemove !== -1; i++){ storyWords.splice(indexToRemove,1); indexToRemove = storyWords.findIndex(word => word === ''); } } //8.2 let findMaxWord = function () { let wordCount; let maxCount = 0; let maxWord; storyWords.forEach (wordOutter => { wordCount = 0; storyWords.forEach (wordInner => { if (wordInner.toLowerCase() === wordOutter.toLowerCase()) { wordCount ++; } if (wordCount > maxCount) { maxCount = wordCount; maxWord = wordOutter; } }) }) console.log('\n------------------------\nThe most apeared word\n\nThe word "' + maxWord + '" is the one that most appeared.') console.log('The total of occurrences were ' + maxCount + ' times in the story.'); } //8.3 const newOverusedWords = ['certainly','super', 'simply']; //new better words let newBetterWords = betterWords; const overWriteOverUsedWords = word => { // overwrite better words for (let i = 0; i < overusedWords.length; i ++) { //loop betterWords for (let j = 0; j < newBetterWords.length; j ++) { if (newBetterWords[j] === overusedWords[i]) { newBetterWords[j] = newOverusedWords[i]; } } } console.log('\n---------------------------------\nThe Story With New Overused Words\n\n' + newBetterWords.join(' ')); } //Call functions // Fist call logInfo, before remvoving overusedWords console.log ('\n------------------------\nFirst call of logInfo()'); logInfo(); // Calling removeOverusedWords() removeOverusedWords(); // second call logInfo, after removing overusedWords console.log ('\n------------------------\nSecond call of logInfo()'); logInfo(); findMaxWord(); overWriteOverUsedWords();


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' ];

// 1.
let storyWords = story.split(' '); 

// 2.

console.log ('There are ' + storyWords.length + ' words in the story\n');

// 3.

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

// 4.

// It counts overused words and returns an array with results.
// First, It creates the function overusedCounter and declares 2 variables:
//    1. report object: stores the data for returning. 
//    2. overusedOccurencies: stores an array with all occurrences of each overused word in storyWords.
// It loops overusedOccurrences and update report object.
// If the property already exists in report, for the current word in overusedOccurrences,
// it increments the property that already exists.
// if not, it creates the property defining its initial value to 1.

let overusedCounter = function() {
  let report = {};
  let overusedOccurrences = storyWords.filter(word => overusedWords.includes(word));
  overusedOccurrences.forEach(word => {
    if (report.hasOwnProperty(word)) { 
      report[word] ++ 
    } else {
      report[word] = 1; 
      }
    }
  );
return Object.entries(report);
}

/* another version also works out.

const overusedCounter = () => {
	let occurrences = [];
	let arrReturn = [];
	for (i = 0; i < overusedWords.length; i++) {
		  occurrences.push(0);
	}
	for (i = 0; i < overusedWords.length; i++) {
  	for (j = 0; j < storyWords.length; j++) {
      if (overusedWords[i] === storyWords[j]) {
        occurrences[i]++
    	}
  	}
  }
	for (i = 0; i < overusedWords.length; i++) {
		  arrReturn.push([overusedWords[i],occurrences[i]]);
	}
	return arrReturn;
}
*/

// 5. 

// For counting sentences, It first filters the storyWords array. 
// The creteria is .includes() punctuation characters as arguments.
// At last, It returns the wordsPunctuated array with .length property 
// meaning the numbers of sentences.
let countSentences = function () {
  let sumWordsPunctuated = storyWords.filter(word =>  
    word.includes('.') || 
    word.includes('!')
  )
  return sumWordsPunctuated.length;
}

//6. 
// Accepting the challenge :) , a logInfo() function with formated output.

const logInfo = () => {
  console.log (`\nThe story has: \n  ${storyWords.length} words, \n  ${countSentences()} sentences and \n  the following overused words:`);

  // It calls overusedCounter() forEach method to show overused words totals.  
  // x[1] is the number of occurrences.
  // x[0] is the word overused.
  // i is the array index, given by the .forEach method. 
  // inside forEach, an if statement for treaten plural and singular outputs
  overusedCounter().forEach ((x,i) => {
    if (x[1] > 1) { 
      console.log(`    ${i+1}. "${x[0]}": ${x[1]} occurrences.`) 
    } else {       
      console.log(`    ${i+1}. "${x[0]}": ${x[1]} occurrence.`)
    }
  });
}

// 7.

// logging betterWords
console.log(`\n\The Better Words\n\n ${betterWords.join(' ')} \n`);

// 8.1

// This function remove overused words.
// firstOverusedInStoryIndex array stores the indexes of 
// the first occurrences of each overused word in storyWords.
// This is the elements that will be preserved.
// A for loop looks up, in storyWords, the index of the current overused word and 
// adds it to firstOverusedInStoryIndex.
// Then, an output not demanded by the task but I include just for montoring 
// how function works out.

const removeOverusedWords = () => {
  let firstOverusedInStoryIndex = []; 
  for (let i = 0; i < overusedWords.length; i++) {
    firstOverusedInStoryIndex.push(storyWords.findIndex(word => word === overusedWords[i]));
    }
  console.log ('\n------------------------\nRemove Overused Words\n\nThe indexes of the first occurrences of each overused words are ' + firstOverusedInStoryIndex + '\n');

  // A second loop process the overwriting of the elements in storyWords 
  // which are overused words, except the first occurrence.
  // It checks, in storyWords, if the current element is included in overusedWords and
  // if is not the first occurence.
  // Then, another output just for monitoring. 
  // So it overwrites the value of the element stored on the index 
  // of the current word, without removing the element, writing an empty 
  // to avoid altering the index of the subsequent overused words occurencies in storyWords. 
  for (let j = 0; j < storyWords.length; j++) {
    if (overusedWords.includes(storyWords[j]) && !firstOverusedInStoryIndex.includes(j)) {
      console.log ('"' + storyWords[j] + '" on index ' + j + ' of storyWords is being removed...')

      storyWords[j]='';
        }
      } 

  // This third loop iterates on storyWords removing empties. 
  // As the search is for the same value, i.e. an empty element, 
  // there is no problem on modifying the subsequent indexes of 
  // the subsequent empties.
  let indexToRemove = storyWords.findIndex(word => word === '');
  for (let i = 0;indexToRemove !== -1; i++){
    storyWords.splice(indexToRemove,1);
    indexToRemove = storyWords.findIndex(word => word === '');
    }
  }

//8.2

let findMaxWord = function () {
  let wordCount;
  let maxCount = 0;
  let maxWord;
  storyWords.forEach (wordOutter => {
    wordCount = 0;  
    storyWords.forEach (wordInner => {
      if (wordInner.toLowerCase() === wordOutter.toLowerCase()) {
        wordCount ++;
        }
      if (wordCount > maxCount) {
        maxCount = wordCount;
        maxWord = wordOutter;
      }
    })
  })
  console.log('\n------------------------\nThe most apeared word\n\nThe word "' + maxWord + '" is the one that most appeared.')
  console.log('The total of occurrences were ' + maxCount + ' times in the story.');
}

//8.3

const newOverusedWords = ['certainly','super', 'simply']; //new better words
let newBetterWords = betterWords;
const overWriteOverUsedWords = word => { // overwrite better words
  for (let i = 0; i < overusedWords.length; i ++) { //loop betterWords
    for (let j = 0; j < newBetterWords.length; j ++) {
      if (newBetterWords[j] === overusedWords[i]) {
        newBetterWords[j] = newOverusedWords[i];
      }
    }  
  }  
console.log('\n---------------------------------\nThe Story With New Overused Words\n\n' + newBetterWords.join(' '));
}

//Call functions

// Fist call logInfo, before remvoving overusedWords
console.log ('\n------------------------\nFirst call of logInfo()');
logInfo();

// Calling removeOverusedWords()
removeOverusedWords();

// second call logInfo, after removing overusedWords
console.log ('\n------------------------\nSecond call of logInfo()');
logInfo();

findMaxWord();
overWriteOverUsedWords();

Giving feedback for the first time? AWESOME! Check out this video for a boost of confidence: How to Review Someone Else’s Code
Feel free to remove this message before posting.

Cheers!
—Codecademy Community Managers

1 Like

Wow, it works perfectly my congratulations :tada:

1 Like