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

152
Vistas
Converting string which have space in it to numbres

I am making a calculator where user can past numbers with spaces like 20 30 40 60 50

and it will calculate all in total = 200

How I can convert this string "20 30 40 60 50" to Numbers with spaces? Because I'll than replace the space with +

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

0

You can use split with map for transform string to number then use reduce for sum array like:

const string = '20 30 40 60 50';
const arrayNumbers = string.split(' ').map(el => parseInt(el));
const sum = arrayNumbers.reduce((sumCounter, a) => sumCounter + a, 0);
console.log(arrayNumbers, sum);

Please note: If you plan to use decimals use parseFloat instead of parseInt

Reference:

  • String.prototype.split()
  • Array.prototype.map()
  • Array.prototype.reduce()
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