Any Idea why this forEach() method isnt working?

Any Idea why this forEach() method isnt working?
0.0 0

#1
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


#2

This is why we recommend not reusing variable names.


#3

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


#4

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


#5

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