www.webdeveloper.com
Results 1 to 5 of 5

Thread: Simple javascript order total calculator not working

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    5

    Simple javascript order total calculator not working

    I'm a complete novice to javascript and I am ready to rip my hair out over this one. I'm supposed to make a webpage that, upon loading, asks the user their name, what product they want to purchase, what the price of that product is, what the discount on that product is and what quantity of that product they would like to purchase. With this information, I am supposed to write javascript that will, at the bottom of my webpage in plain text, return:

    "Thank you for placing an order with us [customer name].
    The cost of buying [quantity] of [product name] is [cost].
    The discount for this purchase is [how much of a discount the customer will be receiving].
    With the discount, your total order cost is [total cost of order with discount]."

    Anything in brackets is a variable that I've defined in my code.

    Here's the code:
    PHP Code:
    <script type="text/javascript">
        var 
    nameproductpricediscountquantity;
        
        var 
    name prompt("Welcome to Ryan's Ice Cream Shop. What is your name?","Enter name");
        var 
    sentence "Hello " name " please look through our available products and services before placing your order.";
        
    alert(sentence);
        
        var 
    product prompt("Please enter the name of the product you are looking to purchase from the table.","Enter product");
        var 
    price prompt("Please enter the price that corresponds to the product you would like to purchase from the price column.","Enter price");
        var 
    discount prompt("Please enter the discount that corresponds to the product you would like to purchase from the discount column.","Enter discount");
        var 
    quantity 1*prompt("How many " product " would you like to purchase?","Enter quantity");
        
        var 
    costprice quantity;
        var 
    orderdiscountprice discount quantity;
        var 
    totalcostcost orderdiscount;
        
        
    document.write("Thank you for placing an order with us, " name ".");
        
    document.write("The cost of buying " quanitity "of" product "is" totalcost ".");
        
    document.write("The discount for this purchase is " orderdiscount ".");
        
    document.write("With the discount, your total order cost is " totalcost ".");
    </script> 
    Nothing after document.write("Thank you for placing an order with us, " + name + "."); works. I just cannot figure out what I've done wrong.

    Any help is greatly appreciated.

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    A quick check in the console reveals that quanitity is undefined. (Misspelling of quantity.)

    The console is your friend! ;)

  3. #3
    Join Date
    Oct 2012
    Posts
    5
    I cannot even begin to tell you how much I appreciate that. I would have never seen that cause I was looking at it for so long that I wasn't even noticing that I misspelled a word. It all works now, but all of those document.write appear in one line. Is there a line break for javascript?

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,272
    Use document.writeln(), like on this w3schools.com page !
    But a form and the use of document.getElmentById (to insert the response in a paragraph) would be better...

  5. #5
    Join Date
    Oct 2012
    Posts
    5
    Thanks guys I really appreciate it

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles