-1

I have created a calculator but it has such problem
var x=060; var y=60; console.log(x+y);
The output is 108 why so?

And, How can i make calculator to enter single operator and wait for an operand and then an operator could appear in.
This is my calculator Calculator2.0

Sami Khan
  • 31
  • 1
  • 8

1 Answers1

3

Put 0 in front of a number means the base is octal. In your case 060 (octal) = 48 (decimal). You add 60 + 48 that is equal to 108.

Titouan56
  • 6,932
  • 11
  • 37
  • 61
  • 1
    *"Put 0 in front of a number means the base is octal."* That format was only ever an "allowed extension" and is expressly forbidden in strict mode. In modern JavaScript, octal is written with a `0o` prefix (just like hex is written with an `0x` prefix and binary with `0b`); http://www.ecma-international.org/ecma-262/7.0/index.html#sec-literals-numeric-literals. – T.J. Crowder Jun 21 '17 at 13:25
  • thanks for precisions – Titouan56 Jun 21 '17 at 13:29
  • but in my calculator if a user input it the same then the functionality of the operator will not be counted.. how can i make changes to it that the zero gets neglected – Sami Khan Jun 21 '17 at 13:29
  • You can simply check the input given by the user and remove the leading '0' – Titouan56 Jun 21 '17 at 13:30
  • @SamiKhan: If you're expecting an integer, use `parseInt(theString, 10)` to force the radix (number base) to decimal. If you're expecting floating point, use `parseFloat`, which always works in decimal. Note, however, that in both cases they happily ignore non-number characters following the number, e,g, `parseFloat("060abc")` is 60. So you may need some pre-validation. – T.J. Crowder Jun 21 '17 at 13:43