Any Idea why this forEach() method isnt working?

function extractLongWords(sentence) {
  // This function takes a sentence with no punctuation
  // It should return an array of all the words of 7+ characters

 var arrayOfWords = sentence.split(' ');
 console.log(arrayOfWords);
 var longWords = [];

 function longWords (item)
 {
   var itemLength = item.length;
   if(itemLength > 7 )
   {
     console.log(item);
     longWords.push(item)
   }
 }
 

  arrayOfWords.forEach(longWords)

  console.log(longWords)




}


 var sent1 = 'at this longitude everything is effervescent'
extractLongWords(sent1)

image

This is why we recommend not reusing variable names.

1 Like

Your problem is that you’ve defined longWords twice

You’ve actually declared it twice then set it to two different things, causing at best undefined behaviour or total failure, and at worst incredibly subtly wrong behaviour

Just be a bit more careful with variable names and you’ll be golden

Edit: aaaand sniped

1 Like

lol :smile: my bad. I hate when this happens and I’ve spent a while typing up my answer. lol

1 Like

heh not your fault, I went to go chuck it in vim to check it out with a linter and when I got back to type I must have missed the icon of you replying