I tried to add two numbers in Javascript:
var output;
output = parseInt(a)+parseInt(b);
alert(output);
It gives wrong output
value, e.g. if a = 015
, and b = 05
. Why is this so?
Expected result of above example should be 20.
I tried to add two numbers in Javascript:
var output;
output = parseInt(a)+parseInt(b);
alert(output);
It gives wrong output
value, e.g. if a = 015
, and b = 05
. Why is this so?
Expected result of above example should be 20.
in many programming languages numbers starting with leading 0 indicate base 8 (octal) representation of the number. here you are giving octal numbers as input and expecting the output in decimal and thats the reason you say output is wrong (which is correct!! wrt octal addition)
solution 1 : you can add two octal numbers and convert the result to decimal
solution 2 : convert the octal numbers to decimal and then add them