/    Sign up×
Community /Pin to ProfileBookmark

Javascript calculator using else if statements

I’ve been tasked with making a webpage with a simple order calculator except I need to use else if statements so that when the user enters a product, it will automatically retrieve what the price and discount are and will calculate their order total. This week you have to write a JavaScript program to allow user inputs and provide some basic error checking. A user should be able to place an order after browsing a list of products and services listed by the company. Depending on the quantity of the product chosen you have to give the user a discount and a total for the purchase.<o:p></o:p>

[QUOTE]

1. Create an HTML file and give it all of the necessary basic tags.
2. Give the page a title of &#8220;JavaScript Assignment 2: [Your name]&#8221; using the title tag.
3. Give the page a heading of &#8220;[Your Business Name] &#8211; eCommerce&#8221;.
4. Include your name, your TA’s first name, the assignment name and the due date in a Javascript multi-line comment
5. Have a table with three columns with headings: Product/Service Name, Price and Discount.
6. Include at least 4 products or services in this table, each on its own row. Each product&#8217;s or service&#8217;s information should appear on its own row in the table.
7. The Discount is going to be the percentage that your business will take off of the total purchase price for a given product. This will be some number between 1% and 50%. However, you will display the Discount as a decimal. This means that you would use a number equal to the Discount divided by 100. eg 10% discount will be written in the HTML table as 0.10, an 8% discount would be written as .08.

  • 1.

    Note: this will make your job easier when you need to do calculations in your code

  • 8.

    Now start your JavaScript code by placing your script tags beneath the table you created in #5


  • 9.

    Have a prompt box to ask the customer for their name. Your prompt should have the message &#8220;Welcome to [Your Business Name]. Please enter your name&#8221;. Save the customer&#8217;s name in a variable. Use your business&#8217; name, where appropriate.


  • 10.

    Next have a welcome message using an alert box. For example: &#8220;Hello [customer name here]. Please look through our available products and services before placing your order.&#8221;


  • 11.

    Have two prompt boxes to take as input the name of the product/service the customer wants from the table and the quantity they want to purchase.


  • 12.

    Have a confirm statement verifying the placed order. For example your Confirm box should show the message:
    Jim you ordered 2 of Ice Cream Cone. Is this correct?


  • 13.

    If the user clicks on CANCEL, the program stops and a message is displayed saying:

  • Refresh the page to reload and place a new order.

  • 1.

    Hint: This will most likely occur in an else branch

  • 14.

    If the user clicks OK, proceed to calculating the cost of purchase.


  • 15.

    Use the if-else structure to determine what the price and discount of the product is based on the product name entered by the user.

  • 1.

    Note: Assume the product name will be entered in as all lower-case characters.

  • 2.

    Hint: You will have an if-else condition for each possible product/service; the statements that run for each of these conditions will set the discount and price.

  • 16.

    If a user enters a product name that does not match the choices displayed in the table, display the following message before quitting from the program.

  • 1.

    Hint: This can be done in the final else part of the if- else structure that looks up the product price and discount.

  • Sorry, Jim. You entered an invalid product. Refresh the page to reload and place the order again.

  • 17. Based on these inputs calculate the cost of the purchase using the formula:
  • cost_of_order = price_of_product * quantity_required

    discount = price_of_product * item_discount * quantity_required

  • 18. Finally have an output using the document.write. The format should be as follows:
    (The bold face words below represent either user-supplied inputs stored in variables or the result of calculation )
  • Thank you for placing an order with us, Jim.

    The cost of buying 2 of Ice Cream Cone is $5.32.

    The discount for this purchase is $ .53

    With discount, your total order cost is $4.79.

  • 1. Note: If you do not have a valid product name, make sure this last item does not print. You can do this by having an if-structure around this output that runs only if a valid product name has been entered.
  • (Note: Each output sentence needs to be printed on a separate line.)

    [/QUOTE]

    Here is my code thus far:

    [code=php]<script type=”text/javascript”>

    var name, product, price, discount, quantity;

    var name = prompt(“Welcome to . 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 quantity = 1*prompt(“How many ” + product + ” would you like to purchase?”,”Enter quantity”);

    var cost= price * quantity;
    var orderdiscount= price * discount * quantity;
    var totalcost= cost – orderdiscount;

    a = confirm(+ name + “, you ordered ” + quantity + ” of ” + product + “. Is this correct?”);

    if (a)
    {
    total= cost – (price * discount * quantity);
    if (product == “ice cream cake”)
    {
    price = 20;
    discount = .15;
    }
    else if (product == “ice cream cone”)
    {
    price = 3;
    discount = .01;
    }
    else if (product == “small ice cream sundae”)
    {
    price = 5;
    discount = .05;
    }
    else if (product == “large ice cream sundae”)
    {
    price = 6;
    discount = .05;
    }
    else if (prompt=(“Sorry, ” + name + “. You entered an invalid product. Refresh the page to reload and place the order again.”))
    {}
    }
    else
    {
    alert(“Refresh the page to reload and place a new order”);
    }

    [U]document.write(“Thank you for placing an order with us, ” + name + “.”);
    document.write(“</br>”);
    document.write(“The cost of buying ” + quantity + ” of ” + product + ” is ” + cost + “.”);
    document.write(“</br>”);
    document.write(“The discount for this purchase is ” + orderdiscount + “.”);
    document.write(“</br>”);
    document.write(“With the discount, your total order cost is ” + totalcost + “.”);[/U]

    </script>
    [/code]

    I’ve gotten up to #15 on my list of instructions and that’s where it begins to get really tough for me. First of all, even when I enter the valid product names based on my table, and the quantity I want, it returns this (I underlined the section of code that corresponds to what a wrote below for easy finding):

    [QUOTE]

    [U]Thank you for placing an order with us, Enter name.
    The cost of buying 2 of ice cream cake is NaN.
    The discount for this purchase is NaN.
    With the discount, your total order cost is NaN. [/U]

    [/QUOTE]

    I have no idea what NaN means or why my code isn’t returning a numerical value. Also, even when I enter something that does not correspond to my products, the code continues running instead of cancelling like it’s supposed to in #16. Anyway, if anyone could help me sort out these problems and finish up this code, I’d really appreciate it.

    to post a comment
    JavaScript

    0Be the first to comment 😎

    ×

    Success!

    Help @LooseJuice spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...