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

188
Visualizações
Splice method don't properly

My splice method don't work here (don't give my code attention just I want to know why this code don't work in my console).

function capSpace(txt) {
  // write your code here
  wordSplit = txt.split("");
  for (let i = 0; i < wordSplit.length; i++) {
    if (wordSplit[i].toUpperCase() == wordSplit[i]) {
      wordSplit.splice(5, 0, " ")
    }

  }
  return wordSplit
}

console.log(capSpace("fausJkalMalkihkLhb"));

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

First of all, you need to increment the i for each character that you add to the array.

I assume you're trying to add a space before each capital letter, in which case you need to add it at the i index, not the 5th index.

function capSpace(txt) {
  // write your code here
  wordSplit = txt.split("");
  for (let i = 0; i < wordSplit.length; i++) {
    if (wordSplit[i].toUpperCase() == wordSplit[i]) {
      wordSplit.splice(i, 0, " ");
      i++;
    }

  }
  return wordSplit
}

var result = capSpace("fausJkalMalkihkLhb").join('');
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

The splice method is adding an empty space to the wordSplit array in your snippet found here:

wordSplit.splice(5, 0, " ")

This is incrementing the length of wordSplit on each iteration and the condition of the for loop:

i < wordSplit.length

will never be satisfied because the length of wordSplit increases which creates an infinite loop and therefore is never able to return wordSplit and therefore never able to console.log the variable of result because it's stuck in an infinite loop.

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