0

I am trying to make my program work, but something is wrong. I want to do a simple tax program, where I am calculating the total price including tax on X amount of items. I want to enter the values using prompt command in JavaScript. Here is my code:

let phonePrice;
let quantity;
let tax;
let total;
let total2;

phonePrice = prompt('Enter the phone price here...','');
quantity = prompt('Enter quantity here...','');
tax = phonePrice * 0.05;
total = phonePrice + tax;
total2 = quantity * total

alert('The Price on ' + quantity + ' phones with tax included is ' + total2 + ' dollars.');

Everything works fine until I want to add decimal number as value. Quantity can't be decimal number of course, but, for example, the item price can be 119.95 dollars. When I enter this number directly in ti the script (without prompt) everything is fine, but when i declare prompt and enter the same value in the browser, it give me NaN as value which is not a number. Anyone got any explanation, and how can I fix this?

  • 1
    You get a string from the input not a number, so you need to convert to number. But you should only use **integers** when doing financial calculations! Do all your calculations in cents instead of in dollars! Integers are reliable, floating point values are not! _Never_ do financial math with fractions. Anyone giving you a solution based on what you are doing instead of telling you that the whole approach is bad is doing it wrong. Links: 1) https://stackoverflow.com/q/3730019/544779 ------ 2) https://stackoverflow.com/q/2876536/544779 – Mörre Nov 25 '17 at 08:49
  • Mörre is of course correct, but this seems like a homework kind of beginner question. This isn't a productive financial application. – Ingo Bürk Nov 25 '17 at 09:14
  • Yeah, I understand, but i am beginner, and this as a homework :). But now I got another question. As far as i know in JavaScript there is no such think as integer or float, like in Java for example. We were told that in JS every number is decimal. Is that correct? – Viktor Jakovlev Nov 25 '17 at 09:38

2 Answers2

1

Try to debug your code, and you will see that problem not in parsing :)

You have STRING in phonePrice (the result of promt is string, let it be '10.5')
Then you have (tax = phonePrice * 0.05;) Result will be float (autoconverted) = 0.525
Then you have (total = phonePrice + tax;) Result will be STRING = "10.50.525" - because in '+' operator , if any argument STRING - all will be used as STRINGs

And then you got NaN in last '*' - because no one know the number "10.50.525" :) and result of float * NaN = NaN

Just try to convert input values.

let phonePrice;
let quantity;
let tax;
let total;
let total2;

phonePrice = parseFloat(prompt('Enter the phone price here...',''));
quantity = parseFloat(prompt('Enter quantity here...',''));
tax = phonePrice * 0.05;
total = phonePrice + tax;
total2 = (quantity * total).toFixed(2); // Needed for correct result to money conversion

alert('The Price on ' + quantity + ' phones with tax included is ' + total2 + ' dollars.');
Muritiku
  • 222
  • 1
  • 7
0

You have to convert the input from the prompt, which is parsed as a string, into a number, with parseFloat and parseInt:

let phonePrice;
let quantity;
let tax;
let total;
let total2;

phonePrice = parseFloat(prompt('Enter the phone price here...',''));
quantity = parseInt(prompt('Enter quantity here...',''));
tax = phonePrice * 0.05;
total = phonePrice + tax;
total2 = quantity * total

alert('The Price on ' + quantity + ' phones with tax included is ' + total2 + ' dollars.');

That said, you should instead count your money in cents instead of dollars, and use integers instead of floats, to avoid errors with floating-point accuracy.

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76