Count matching words between arrays

Hi there! i’m doing an exercise which asks me to create a higher order function to spot some words (let overusedWords = [‘really’, ‘very’, ‘basically’]) in another array(let betterWords).

I thought doing it like this but the function return ‘undefined’ when i call it…

const countOverusedWords = betterWords.forEach(word => {
  let countReally = 0;
  let countVery = 0;
  let countBasically = 0;

  if(word === overusedWords[0]) {
    countReally++;
  } else if (word === overusedWords[1]) {
    countVery++;
  } else if (word === overusedWords[2]) {
    countBasically++;
  }
  return 'Really = ' + countReally +', Very = ' + countVery + ', Basically = ' + countBasically;
});

It has to be made by using a higher order function… Can please anyone spot the mistake??

Thanks in advance!

Hey @aotab91,

Just a quick question, did you define overusedWords?

1 Like

I did!

Both overusedWords and betterWords are previously defined… I just didnt copy them here as betterWords contains +100 words…

Can you give us those variables anyways? So maybe I can troubleshoot it on an editor. Thanks :slight_smile:

1 Like

Absolutely, that’s very nice of you.

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


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

const betterWords = storyWords.filter(word => {
  if(unnecessaryWords.includes(word) === false) {
    return word;
  }
});
console.log(betterWords);


const countOverusedWords = betterWords.forEach(word => {
  let countReally = 0;
  let countVery = 0;
  let countBasically = 0;

  if(word === overusedWords[0]) {
    countReally++;
  } else if (word === overusedWords[1]) {
    countVery++;
  } else if (word === overusedWords[2]) {
    countBasically++;
  }
  return 'Really = ' + countReally +', Very = ' + countVery + ', Basically = ' + countBasically;
});

console.log(countOverusedWords);

So I first created a new array called betterWords splitting the story string into an array of separated words, eliminating the unnecessaryWords from it.

So now the second step would be counting how many times the words in the overusedWords array appear in the betterWords array

Well, first of all:

  • Putting the variables countReally = 0, countVery = 0, and countBasically = 0; inside the forEach function will cause the variables to just restart at 0 again on every word. So if you want to truly count them, you have to put them outside the .forEach function.
  • The countOverusedWords will return undefined because the .forEach function doesn’t return anything. It’s just a way to loop through an array and do something to it. So a solution would be to create an empty variable, then do something like this:
Variable = 'Really = ' + countReally +', Very = ' + countVery + ', Basically = ' + countBasically;

//Or if you want to use template strings
Variable = `Really = ${countReally}, Very = ${countVery}, Basically = ${countBasically}`;

Hope this helps :slight_smile:

1 Like

Awwww ok so basically, the .forEach function doesn’t return any result, it simply iterates through an array and does something to it.

So if i wanted to log a sentence with the results i would have to do it with a console.log() statement after the function.

I’ll keep in mind as well setting global scope variables instead of declaring them inside a higher order function.

That really helped! Thanks very much for taking your time with this!!

let countReally = 0;
let countVery = 0;
let countBasically = 0;

const countOverusedWords = betterWords.forEach(word => {

  if(word === overusedWords[0]) {
    countReally++;
  } else if (word === overusedWords[1]) {
    countVery++;
  } else if (word === overusedWords[2]) {
    countBasically++;
  }
});

console.log(`Really = ${countReally}, Very = ${countVery}, Basically = ${countBasically}`);

Now it works!!