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?

3
  • 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) stackoverflow.com/q/3730019/544779 ------ 2) stackoverflow.com/q/2876536/544779 Commented Nov 25, 2017 at 8:49
  • Mörre is of course correct, but this seems like a homework kind of beginner question. This isn't a productive financial application. Commented Nov 25, 2017 at 9: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? Commented Nov 25, 2017 at 9:38

2 Answers 2

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.');

Sign up to request clarification or add additional context in comments.

1 Comment

TY :) this was very helpful. I understand everything now.
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.

1 Comment

He should not use floats for financial calculations. Any answer should tell him that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.