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

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

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 Relatório

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 Relatório

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