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

192
Vistas
Why is .splice(1, 0, '.') returning an empty array? (Javascript)

Here is my code:

let num = 0.0134;

console.log(num.toString().split('').splice(3).splice(1, 0, '.'))

The console.log returns an empty array.

I want it to return '1.34'. What am I doing wrong?

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

0

If you see the reference for Array.prototype.splice on mdn:

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place

Return Value: An array containing the deleted elements.

At the end of your code, when you do .splice(1, 0, '.'), it deletes 0 elements so it will return an empty array.

I'm assuming you want to get the previous array with the '.' element inserted in the first position. For that you'll want to do:

const arr = num.toString().split('').splice(3);
arr.splice(1, 0, '.');
console.log(arr)

And If you want to join it to a string just use arr.join('')


Anyway, for your use case, you could just use the * multiplication operator rather than converting to a string array and attempting to manipulate that.

You can simply do:

let num = 0.0134;
console.log(num * 100);
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