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

405
Vistas
Why am I getting an error when using split function of javascript?

I'm trying to split my string but not get proper output as per my expected.

Input

let name = "helloguys";

Output

h
he
hel
hell
hello
hellog
hellogu
helloguy
helloguys
let name = "mynameisprogrammer";
for (let index = 1; index <= name.length; index++) {
  let finalData = name.split(name[index]);
  console.log(finalData[0]);
}

enter image description here

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use substring to take parts of your input:

let name = "mynameisprogrammer";
for (let index = 1; index <= name.length; index++) {
  let finalData = name.substring(0, index);
  console.log(finalData);
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use Array.map() along with String.slice() to split the string into the required values:

let name = "mynameisprogrammer";
let result = [...name].map((chr, idx) => name.slice(0, idx + 1));

console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you need this to be unicode aware, you might want to avoid using string substring / slice etc.

Instead convert to array, and split there.

eg..

let name = "my😀name🤣is🫠programmer👍";

const nameA = [...name];
for (let ix = 1; ix <= nameA.length; ix += 1) 
   console.log(nameA.slice(0, ix).join(''));
.as-console-wrapper { max-height: 100% !important; }

ps. To see what I mean by unicode aware, try using the name from the code above into some of the other answers.

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