Skip to content Skip to sidebar Skip to footer

Javascript Calculator 5 + 5 = 55?

Hey I made a javascript calculator and want the to try 5 + 5 it give me 55 other then 10 ? How ever if I try 5 / 5 it give me 1 fine if i try 5 - 5 it gives me 0 fine and same with

Solution 1:

N1 and N2 are strings, not numbers. Adding two strings together concatenates them, which is what your "error" is.

You need to parse them into numbers:

varN1 = Number(prompt("Enter first number"));

Solution 2:

The reason this works for other operators but not plus is because "a" + "b" = "ab".

That said, you're adding strings. So "5" + "5" = "55"

If you first convert them to integers, it should fix this.

var sum = (parseInt(N1,10) + parseInt(N2,10));

Solution 3:

Prompt gets you the string format of input , you need convert it into integer for integer add operation.

Post a Comment for "Javascript Calculator 5 + 5 = 55?"