Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda