Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Basic help, bit confused - HELP!

1. ## Basic help, bit confused - HELP!

So I am basically as new as it gets when it comes to HTML and JavaScript and all that, not even sure if it's the right section. (Sorry if this is the wrong section)

Create a const with an assigned tax rate value of 6.5%
Create a variable to store a subtotal amount of \$329.03
Create variable for the computed sales tax amount
Create variable for the total sale amount
Write an algorithm that calculates the raw sales tax value
Use the parseFloat().toFixed() method to format sales tax to two decimal values (be careful, the variable is now a string!)
Calculate the total sale by adding the sales tax to the subtotal (be sure to use parseFloat() method for sales tax, it is in string format)
Use the parseFloat().toFixed() method to format the value of the total sale variable to use two fixed decimal places
Display to the user the subtotal, sales tax, and total sale amount on three separate lines identifying each data attribute

That's basically everything I need to do for a class, however I cannot access the notes for some odd reason so I'm basically stuck playing a guessing game. It's not due anytime soon, I just like to get a head start on things.

• This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.

Pretending that other people's work is your own is normally regarded as cheating. Dishonest, in fact.

Double posting is banned - see forum rules.

Quizmaster: Which month of the year is named after Julius Caesar?
Contestant: August

• Hi Clumzy, Well half of what you are asking about involves defining some variables and the other half is using them with some functions and displaying results. Check out these links to get a quick idea of how to create the variables...

https://developer.mozilla.org/en-US/...,_and_literals

Then once you see how that works, you can read more about using the toFixed method at the link below...

https://developer.mozilla.org/en-US/...Number/toFixed

Mix these two together and you already have most of your solution.

I hope this helps!

• Thank you so much, and I did not make it seem like I wanted to see the exact code.

I found out about taxrates, declaring them as a const in all caps and all that.

so the beginning in the html coding would be something like:

const TAXRATE = .065; (I think)

however I do not know what to even start to write to "create a variable to store a subtotal amount of __"

• Hi Clumzy,

The keyword "const" is used in Emacscript 1.5 (Javascript) but due to issues with support in browsers like IE, it is preferred not to use it. But you are on the right track with the capital letters etc. As for creating a variable to store a subtotal, I assume you know what a subtotal is... adding up all the items prices before applying any taxes. So all you need is something like... var subtotal = price1 + price2 + price3; Given that price1 through 3 were product prices we were adding up to create a subtotal.

• I understand what a subtotal is, I just figured you would have to state the price1 or whatnot to then add in the var subtotal.

Aka something like var price1 = \$30; var price2 = \$22.30. So then Var subtotal would be something like connecting those two prices and adding in the var TAXRATE, which I'm a bit confused about.

• Originally Posted by Clumzy
I understand what a subtotal is, I just figured you would have to state the price1 or whatnot to then add in the var subtotal.

Aka something like var price1 = \$30; var price2 = \$22.30. So then Var subtotal would be something like connecting those two prices and adding in the var TAXRATE, which I'm a bit confused about.
\$22.50 with a \$ sign prefix is a string value (not a number) so you cannot perform arithmetic on it. In any case, as a string it must be in quotes.

var price1 = 30; // a number
var price2 = 22.5;
var price3 = 23.75;
var taxrate = .065; // 6.5%
var subtotal = price1 + price2 + price3; // add them all up
var tax = subtotal * taxrate;
var total = (subtotal + tax).toFixed(2); // note the brackets - total is now a string value
total = "\$" + total; // \$ is concatenated not added

I do not see how parseFloat() comes into it as the values on which the arthmetic is performed are numbers already. Formatting to 2DP and with a dollar sign prefix should be the very last step in your workings, after all other arithmetic has been completed.

• Originally Posted by Philip M
\$22.50 with a \$ sign prefix is a string value (not a number) so you cannot perform arithmetic on it. In any case, as a string it must be in quotes.

var price1 = 30; // a number
var price2 = 22.5;
var price3 = 23.75;
var taxrate = .065; // 6.5%
var subtotal = price1 + price2 + price3; // add them all up
var tax = subtotal * taxrate;
var total = (subtotal + tax).toFixed(2); // note the brackets - total is now a string value
total = "\$" + total; // \$ is concatenated not added

I do not see how parseFloat() comes into it as the values on which the arthmetic is performed are numbers already. Formatting to 2DP and with a dollar sign prefix should be the very last step in your workings, after all other arithmetic has been completed.
Thank you for the explanation -- what exactly does .toFixed(2) do, I haven't heard of anything remotely close to that?

Also, so parse is basically for things that are not numbers to convert them back into numbers (if that makes any sense)? I forgot that you cannot add a \$ in front of the number though, I remember that -- thanks for the reminder though.

• .toFixed() does what it says on the tin - converts a number to a string value with that many decimal places.

var x = 123.45678;
x = x.toFixed(2);

The parseFloat() function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string. Remember that values input by the user into text boxes are strings, not numbers, although conversion is often automatic. The danger is when two values which are strings although they look like numbers are concatenated with the + operator, not added. 12+34 = 1234.
However, to convert a string value to a number it is best to use Number(). Number(12+34) = 46.

parseFloat() and Number() can give different results:-

alert (parseFloat("12,345")); // 12 - could create a serious error if 12345 is intended.

•