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

207
Vistas
How to add two numbers without concatenating them as strings?

The parseInt(sum3) + parseInt(sum5) is not adding the two variables. It just concatenates them. How can I add the two?

I know I can just manually create another variable and put the output of sum3 there and put the output of sum5 in another variable and add them like that but I idk I think that's cheating. So how can I add the sum3 and sum5?

// sum of 3
let max3 = parseInt(1000);
let sum3 = 0;

for ( let three=3; three<=max3; three++ ){
    sum3+=three
}


// sum of 5
let max5 = parseInt(1000);
let sum5 = 0

for (let five=5; five<=max5; five++){
  sum5+=five
}


// sum of 3 and 5

document.write("Sum of 3 = " + sum3 + "<br/>"
   + "Sum of 5 = " + sum5 + "<br/>" + 
    "Sum of 3 and 5 = " + parseInt(sum3) + parseInt(sum5));
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It's just because you're using + in the context of string concatenation.

Should be:

document.write("Sum of 3 = " + sum3 + "<br/>"
   + "Sum of 5 = " + sum5 + "<br/>" + 
    "Sum of 3 and 5 = " + (sum3 + sum5));

Or you really should use:

document.write(`Sum of 3 = ${sum3} <br/>
        Sum of 5 = ${sum5} <br/>
        Sum of 3 and 5 = ${sum3 + sum5}`);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can just wrap parseInt(sum3) + parseInt(sum5) in a () bracket and it should do the job.

// sum of 3
let max3 = parseInt(1000);
let sum3 = 0;

for (let three = 3; three <= max3; three++) {
  sum3 += three
}


// sum of 5
let max5 = parseInt(1000);
let sum5 = 0

for (let five = 5; five <= max5; five++) {
  sum5 += five
}


// sum of 3 and 5

document.write("Sum of 3 = " + sum3 + "<br/>" +
  "Sum of 5 = " + sum5 + "<br/>" +
  "Sum of 3 and 5 = " + (parseInt(sum3) + parseInt(sum5)));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You concatenate both string values with the use of '+'.

Don't combine this with string so '+' will still work as expected.

// sum of 3
let max3 = 1000;
let sum3 = 0;

for (let three=3; three<=max3; three++){
    sum3+=three
}


// sum of 5
let max5 = 1000;
let sum5 = 0

for (let five=5; five<=max5; five++){
  sum5+=five
}


// sum of 3 and 5
let totalSum = sum3 + sum5

document.write("Sum of 3 = " + sum3 + "<br/>"
   + "Sum of 5 = " + sum5 + "<br/>" + 
    "Sum of 3 and 5 = " + totalSum);

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