'Check if given string contains 3 words in succession.Using javascript

Here is my attempt but its not working.I am coding it on checkIo website.it is worrking fine on my local editor but failed on checkIO giving error 'your result undefined' for test 'hello world hello ' the string passed can contain number, word mixed with number, only word without number are to be counted in succession.

function wordChecker(text){
let arr=text.split(' ');
  let counter=0;
  for(let i=0;i<arr.length;i++){
     if(isNaN(arr[i])){
       counter++;
       if(counter===3){
         return true
       }
     }
     else {
       counter=0;
     }
  }
  return false;
}

wordChecker('hello world Hello');
wordChecker('hey I don't43 qualify');
'''



Solution 1:[1]

The straight-forward way to go about this is to loop the words, keeping track of the previous word, incrementing a counter when the current word matches the previous one...

function hasNConseqWords(text, n=3) {
  const words = text.split(' ');
  let lastWord = null;
  let successiveCount;
  for (let word of words) {
    if (word === lastWord) {
      successiveCount++;
      if (successiveCount === n) return { n, word };
    } else {
      successiveCount = 1;
      lastWord = word;
    }
  }
  return { n, word: null };
}

console.log(hasNConseqWords('now is the the the time for all good men'))
console.log(hasNConseqWords('now is the the time for all good men to come to'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danh