Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
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');
'''

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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'))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!