Mini Linter Project with array looping demonstration

Here are my implementation to the project of “Mini Linter” with assisting of instructions attached to it, stack overflow and some other code snippets from other places,

// text assignment

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

// split text into single words
let storyWords = story.split(' ');
console.log(storyWords);

// define overused words
let overusedWords = ['really', 'very', 'basically'];

// define unnecessary words
let unnecessaryWords = ['extremely', 'literally', 'actually'];

//filter the unnecessary words out of text in story words array then assign the result to betterWords array 
let betterWords = storyWords.filter(function(word) {
    return !unnecessaryWords.includes(word);
  })

console.log(betterWords) // printing out the text without the unnecessary words

// count overused words
let reallyCount = 0;
let veryCount = 0;
let basicallyCount = 0;

// counting the overused words
for (word of storyWords) {

    if (word === 'really') {
      reallyCount += 1
    } else if (word === 'very') {
      veryCount += 1
    } else if (word === 'basically') {
      basicallyCount += 1
    }

  }

// count sentences
let sentenceCount = 0;

//counting the number of sentences in the text
for ( wordEnd of storyWords) {
    if (wordEnd[wordEnd.length - 1] === '.' || wordEnd[wordEnd.length - 1] === '!') {
  
        sentenceCount += 1
  
    }

  };


// Defin function logging out some statistics about the text
const logInfo = (param1, param2, param3, param4, param5) => {
    param1 = storyWords.length;
    //word no
    console.log('word Count :' + param1);

    //printing out the number of the sentences in the text
    console.log(`The number of the sentences in the text is ${param2}`)

    // printing out the overused words 
    console.log('really :' + param3);
    console.log('very :' + param4);
    console.log('basically :' + param5);
  
};
// function call with 5 arguments
logInfo( storyWords.length,sentenceCount, reallyCount, veryCount, basicallyCount)
//console.log(betterWords.join(' '));

// Defin a random index to choose a word from a set of arrays of replacing words
let randomIndex = Math.floor(Math.random() * 4);

//replace the overused words in the text with some words chosen randomly from three arrays

//replace "really" and printing out the text
while(storyWords.indexOf('really') != -1) {

  const reallyReplacements = ['truly', 'absolutely', 'certainly'];
  storyWords.splice(storyWords.indexOf('really'), 1, reallyReplacements[randomIndex]);

}
//replace "very" and printing out the text
while(storyWords.indexOf('very') != -1) {

const veryReplacements = ['tremendously', 'greatly', 'decidedly'];
  storyWords.splice(storyWords.indexOf('very'), 1, veryReplacements[randomIndex]);

}

//replace "basically" and printing out the text
while(storyWords.indexOf('basically') != -1) {

  const basicallyReplacements = ['essentially', 'fundamentally', 'primarily'];
  storyWords.splice(storyWords.indexOf('basically'), 1, basicallyReplacements[randomIndex]);

}

// printing out the array text as a whole formatted text separated by spaces
console.log(storyWords.join(' '));

//find the most frequent word in the text 
console.log(findMostRepeatedWord(story)); // Result: "the"

// find the most frequent word in the text array
function findMostRepeatedWord(story) {
  let words = story.match(/\w+/g); //match the who;e text 
  console.log(words); 

  // creat an object contains every words with their matching number 
  let occurances = {};
  
  //looping through the words array to count every word frequency
  for (let word of words) {
    if (occurances[word]) {
      occurances[word]++;
    } else {
      occurances[word] = 1;
    }
  }

  console.log(occurances); // printing out the frequency of each word in the text

//finding out the most repeated word in the text
  let max = 0;
  let mostRepeatedWord = '';

  for (let word of words) {
    if (occurances[word] > max) {
      max = occurances[word];
      mostRepeatedWord = word;
    }
  }
//print out the most repeated word in the text
  return `The most repeated word in the text is : ${mostRepeatedWord}`;
}
5 Likes

Nice looking code!

But I feel like the video example for step 4 made a big mistake, and I think you made it here as well.

Step 4 said to count instances of the words in the overusedWords array, but in the video, Brendan didn’t use the array at all, other than to read the values off the screen (not using programming) and just type them in as literals and in variable names like:
let reallyCount = 0;
let veryCount = 0;
let basicallyCount = 0;

The problem with this is that his code would not work for different values of overusedWords. There’s no point in putting these values in an array since the code doesn’t use the array!

Here’s my solution, for what it’s worth, which is less code than Brendan’s, straightforward, and most importantly, uses the array!

image

Here’s how it works:

It loops through the list of overused words.
For each one, it creates a temp array by filtering down the story array to just those words that match the overused word. So now this temporary array contains nothing but each instance of the overused word that was present in the story array.
Log out the number of elements in that temp array, and on to the next overused word!

Output looks like this:

Hey, really was used 2 times!
Hey, very was used 5 times!
Hey, basically was used 1 times!

13 Likes

Wow, this is really nice.

1 Like

I added a “Pluralizer”.

2 Likes

Awesome contribution

1 Like


let senencesCount = 0;

for (wordEnding of storyWords) {

if (wordEnding [wordEnding.length-1]) === “.” || wordEnding [wordending.length-1] === “!”) {

  sentenceCount +=1;

}

};

What am I doing wrong here? Thanks in advance.

there’s some syntax errors;
you need to put let in front of the first wordEnding
and
there’s an extra ) inside the if-statement declaration

Also, check your spelling:
senencesCount or sentenceCount ?
wordEnding or wordending ?

try:

let sentencesCount = 0;
for (let wordEnding of storyWords) {
  if (wordEnding [wordEnding.length-1] === "." || wordEnding[wordEnding.length-1] === "!") {
    sentencesCount +=1;
  }
};

I would recommend using the .split function for strings instead of splitting them yourself in later lessons.

I too was puzzled on this one, and originally used a nested for loop to obtain the counts needed of each item in the overusedWords array. The scope of the exercise is to utilize built in iterators to solve the problems.

I took the solution provided by prismpaul (nice work!) and refactored it to use the .forEach iterator instead of a for loop.

image

1 Like

Thanks @amrmusharrafa8940871 and @prismpaul for these solutions.

However, I’m still struggling with my original code which tried to use the for… of loop (like the Get Unstuck video).

for (word of storyWords) {
  if (word === 'really') {   
    reallyCount += 1;
  } else if (word === 'very') {
    veryCount += 1;
  } else if (word === 'basically') {
    basicallyCount += 1; 
  } 
}

Why won’t my “of” highlight to blue? This is the same code from the Get Unstuck video, but the guy in the video copy and pastes his for some reason. {note: I think it shows up blue in my codebyte, but not when I’m in the codecademy lesson}

Thanks!