0
javascript:
var Value1 = prompt("Value? ", "");
var Value2 = (Value1+0.00003);
var Value3 = (Value1+0.00002);

console.log("Value 1 = " + Value1);
console.log("Value 2 = " + Value2);
console.log("Value 3 = " + Value3);

This just adds 0.0003 at the end of Value1, instead of doing the math. However, when I don't prompt for Value1, but give it a value myself, it all works as it should.

Eg: prompt for Value1 --> 0.8 --> Value2 becomes 0.80.00003 instead of 0.80003.

What should I change to the code?

EDIT:

By adding "Number" as suggested, Values get treated correctly, but the ouytput is not as expected:

Value 1 = 0.8
Value 2 = 0.80003
Value 3 = 0.8000200000000001 

why is this?

Wannes
  • 83
  • 1
  • 3
  • 11
  • `window.prompt` returns a `String`, so you have to convert that to `Number` first. See also: https://developer.mozilla.org/en-US/docs/Web/API/Window.prompt – KooiInc May 20 '14 at 13:43

1 Answers1

2

Value1 is getting saved as a string because its a user input, you just need to make it a number:

var Value1 = Number(prompt("Value? ", ""));
juvian
  • 15,875
  • 2
  • 37
  • 38