Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    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
    Last edited by Philip M; 06-02-2014 at 08:55 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jun 2014
    Location
    Vancouver, Canada
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #4
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 __"

  • #5
    New Coder
    Join Date
    Jun 2014
    Location
    Vancouver, Canada
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  • #6
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by Clumzy View Post
    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.
    Last edited by Philip M; 06-05-2014 at 08:45 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    $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.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    .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);
    alert (x); // 123.46

    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.
    alert (Number("12,345")); // NaN
    Last edited by Philip M; 06-06-2014 at 09:11 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    LinkBacks (?)

    1. 06-02-2014, 08:05 AM

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •