Mini Linter Step 3 - betterWords array empty

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' ]; let storyWords = story.split(' '); console.log(storyWords.length); //betterWords returns empty array, why? let betterWords = storyWords.filter(word => { !unnecessaryWords.includes(word); }); console.log(betterWords); //total count of uses for all overused words let countOverused = 0; storyWords.forEach(word => { if (overusedWords.includes(word)) { countOverused++; } }); let countSentences = 0; storyWords.forEach(word => { dotIndex = word.length - 1; //index of the last symbol in a word if (word[dotIndex] === '.' || word[dotIndex] === '!') { countSentences += 1; /*if word ends in . or ! increase sentence count */ } }); wordCount = storyWords.length; console.log('Word count: ' + wordCount); console.log('Sentence count: ' + countSentences); // function to print how many times a word is used const eachOverusedCount = testWord => { //accepts a word let count = 0; /*let wordIndex = overusedWords.indexOf(testWord); /* determine which overusedWords word it is, to not cycle through all of them*/ storyWords.forEach(word => { if (testWord === word) { count++; /* counts how many times testWord is used in storyWords */ } }); console.log(`'${testWord}' is used ${count} time(s) in the story!`); }; //eachOverusedCount('really'); //eachOverusedCount(overusedWords[1]); //eachOverusedCount(overusedWords[2]); /*function to either log how many times are all words in the unnecessaryWords array used if the parameter is empty, or a specific word count if passed as parameter*/ const logOverused = whichWord => { if (whichWord) { //if a word is passed to the function eachOverusedCount(whichWord); } else { overusedWords.forEach(word => { /*if the function is called without specifying the word, iterate through overusedWords, calling the function to check the count for each word there */ eachOverusedCount(word); })}; }; //logOverused(overusedWords[2]); logOverused(); console.log(betterWords.join(' ')); console.log(betterWords);

Hello, I’m having trouble with betterWords array, please help.

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

let storyWords = story.split(' ');
console.log(storyWords.length);

//betterWords returns empty array, why?
let betterWords = storyWords.filter(word => {
  !unnecessaryWords.includes(word);
});
console.log(betterWords);

//total count of uses for all overused words
let countOverused = 0;
storyWords.forEach(word => {
  if (overusedWords.includes(word)) {
    countOverused++;
  } 
});


let countSentences = 0;
storyWords.forEach(word => {
  dotIndex = word.length - 1; //index of the last symbol in a word
  if (word[dotIndex] === '.' || word[dotIndex] === '!') {
    countSentences += 1; /*if word ends in . or ! increase sentence count */
  }
});


wordCount = storyWords.length;
console.log('Word count: ' + wordCount);
console.log('Sentence count: ' + countSentences);

// function to print how many times a word is used 
const eachOverusedCount = testWord => { //accepts a word 
  let count = 0; 
  /*let wordIndex = overusedWords.indexOf(testWord); /* determine which overusedWords word it is, to not cycle through all of them*/
  storyWords.forEach(word => {
    if (testWord === word) {
      count++; /* counts how many times testWord is used in storyWords */
    }
  });
  console.log(`'${testWord}' is used ${count} time(s) in the story!`); 
};

//eachOverusedCount('really');
//eachOverusedCount(overusedWords[1]);
//eachOverusedCount(overusedWords[2]);


/*function to either log how many times are all words in the unnecessaryWords array used if the parameter is empty, or a specific word count if passed as parameter*/
const logOverused = whichWord => {
  
  if (whichWord) { //if a word is passed to the function
    eachOverusedCount(whichWord); 
  } else {
    overusedWords.forEach(word => { /*if the function is called without specifying the word, iterate through overusedWords, calling the function to check the count for each word there */
    eachOverusedCount(word);
  })};
};


//logOverused(overusedWords[2]);
logOverused();


console.log(betterWords.join(' '));
console.log(betterWords);
















Hi Kate,

You don’t return anything from that function. Either you use the shorthand syntax with implicit return:

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

or you add the return keyword.

1 Like

Thanks, that worked! I guess it’s all a bit jumbled in my mind because I’m going through lessons fast, will have to redo them again.

1 Like