Mini Linter step 4 - 8 need further understanding of alternatives


#1

I was using the project hint to help with the overusedWords iteration loop. In the end I solved it with only one if statement but I have a couple of questions:

  1. Why does the word count become inaccurate if I mover the timesUsed variable outside of the first for loop block to the beginning of the function?
  2. Why does the step hint using an if and two if else statements would have been easier than the solution below? (I’ve left my console test lines in and put in my comments)
  3. Lastly I know this sort of works to print what I want to the console but is it really a correct solution?
//make this a function cos its big
const overusedWordCount = () => {
  // Use the array of overused words as the outer loop x3
  for (let overusedIndex = 0; overusedIndex < overusedWords.length; overusedIndex++){
    let timesUsed = 0; // Set wordcount variable here
    //console.log(`The Word ${overusedWords[overusedIndex]} was used ${timesUsed} times`);  
    
    // Use the betterWords array as the inner loop
    for (let wordIndex = 0; wordIndex < betterWords.length; wordIndex++){
      //console.log(`wordIndex is ${betterWords[wordIndex]}`);
      
      //make the comparison and iterate the word count on match
        if (betterWords[wordIndex] == overusedWords[overusedIndex] ){
          timesUsed += 1;
        } 
      }
    // Display the word count
    console.log(`The word ${overusedWords[overusedIndex]} was used ${timesUsed} times`);
    }
  }

overusedWordCount();

#2

I’ve now realised that I should have used iterator methods so I’ll refactor the code… doh #epicfail #missedthepoint


#3

We’ll keep this topic open in case you still need help with the code.

Mini Linter

The following is not a complete solution, but a test case to consider…

// filter only wanted words
let betterWords = storyWords.filter(word => !unnecessaryWords.includes(word));
console.log(betterWords.length);

// filter overused words
let excessWords = betterWords.filter(word => overusedWords.includes(word));
console.log(excessWords.length);
console.log(excessWords);

#4

Thanks for that but I’m realising my loops are the correct for task 4 way I’m just looking for a more concise way and also to understand how to incorporate if else statements


#5

I’m having serious trouble just figuring out how to refactor I’ve looked in the forums and I’m pretty clueless at this point - other peoples answers just confuse me more and I know its something simple


#6

This is all my code. I still don’t understand how to refactor step four I really would appreciate help here.
I attempted to do it using forEach above loop code that work but the count comes back wrong and I still haven’t figured out how to incorporate if else properly.

Nobody is on the chat and I’m a pro member so I’m a little frustrated that there is no mentor available. I want to understand step 4 alternatives before attempting step 8 extra work

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

//STEP 1 Spilt the story string into an array
let storyWords = story.split(' ');

console.log(`There are ${storyWords.length} words in the original story`);

//STEP 2 Create an array without the unncessary words
let betterWords = storyWords.filter(word =>
 !unnecessaryWords.includes(word));

//STEP 3 Get the word count
console.log(`There are ${betterWords.length} words in the new story`);

//STEP 4.1 Create an array of overused words
let excessWords = betterWords.filter(word =>overusedWords.includes(word));
//console.log(`There are ${excessWords.length} overused words in the new story`);

//STEP 4.2 Find out how many times each words is used (refactored code)

 let excessWordsCount = 0
overusedWords.forEach(word => {  
  excessWords.forEach(item => {
    let ex = excessWords.indexOf(item);
    let ov = overusedWords.indexOf(word);
    if (excessWords[ex] == overusedWords[ov]){
      excessWordsCount += 1;
      }
    
    });
  console.log(`The word ${overusedWords[overusedWords.indexOf(word)]} was used ${excessWordsCount} times`);
});

console.log(excessWordsCount)

//STEP 4.3 ORIGINAL CODE - successful execution 

//make this a function cos its big
const overusedWordCount = () => {
  // Use the array of overused words as the outer loop x3
  for (let overusedIndex = 0; overusedIndex < overusedWords.length; overusedIndex++){
    let timesUsed = 0; // Set wordcount variable here
    //console.log(`The Word ${overusedWords[overusedIndex]} was used ${timesUsed} times`);  
    
    // Use the betterWords array as the inner loop
    for (let wordIndex = 0; wordIndex < betterWords.length; wordIndex++){
      //console.log(`wordIndex is ${betterWords[wordIndex]}`);
      
      //make the comparison and iterate the word count on match
        if (betterWords[wordIndex] == overusedWords[overusedIndex] ){
          timesUsed += 1;
        } 
      }
    // Display the word count
    console.log(`The word ${overusedWords[overusedIndex]} was used ${timesUsed} times`);
    }
  }
overusedWordCount();


// STEP 5 Get the sentence count using a regular expression to count '.' and '!'
let regex = /!|\./g;
let sentenceCount = 0;

betterWords.forEach(word => {
  if (word.match(regex)){
    sentenceCount++
  }
});

//STEP 6 Other logs are written near code above
console.log(`There are ${sentenceCount} sentences`);

//STEP 7 
console.log(betterWords.join(' '))

//STEP 8 todo
//8.1 Would like to understand 4 before attempting
//8.2 Would like to understand 4 before attempting
//8.2 Would like to understand 4 before attempting