Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda