Repeating instead of adding

Asked

Viewed 27 times

-4

Why are you repeating the number instead of adding?

var valor = prompt("calculo")
var valor1 = prompt("adição")

if (valor + 0 || valor1 + 0) {
  document.write(valor += valor1)
} else {
  document.write("valor incorreto")
}

1 answer

0


Every return of the prompt is a string, in which case you are adding 2 string, where concatenation will occur.

You can use parseint() to fix the problem.

var valor = parseInt(prompt("calculo"))
var valor1 = parseInt(prompt("adição"))

if (valor + 0 || valor1 + 0) {
  document.write(valor += valor1)
} else {
  document.write("valor incorreto")
}

  • 3

    There is no need to answer a question that is duplicated, as I mentioned in my comment. If you think you can make an answer that adds value to the community, do it at related question. However, your answer does not differ from the existing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.