Why my script does not add the numbers properly?

Home » Programming & Design » Why my script does not add the numbers properly?
Programming & Design No Comments

So with the following, when I make it multiplication it works perfectly, but when I make it addition, it does not work. Please help.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var one = prompt();
var two = prompt ();
var three = (one *

So with the following, when I make it multiplication it works perfectly, but when I make it addition, it does not work. Please help.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var one = prompt();
var two = prompt ();
var three = (one * two);
window.alert(three);
</script>

</body>
</html>

Best Answer:

THE EAGLE: do it like this
var three = (+one + +two);

http://stackoverflow.com/questions/83774…

Other answer:

THE EAGLE:
It's because prompt() returns text, but you can actually add text. "5" + "6" is "56".

The proper way is to use

var one = Number(prompt());

EddieJ:
Perhaps you can use:

var one = +prompt ();
var two = +prompt ();

Bill:
What happens when you make it addition?

LEAVE A COMMENT