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

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

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 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