Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Pranav Kural
- // Render HTML code for an order summary
- function order_summary(order_object) {
- // Declaring the variables x - cost amount, y - tax amount
- var x, y;
- // Setting the value of x based on the amount of purchase (not using ternary to keep it clear and easy to update or edit in future)
- // Using Switch Statement as it is more appropriate for the purpose of performing task based on the values of an expression.
- switch (order_object.price_level) {
- case "free":
- x= 0;
- break;
- case "discount":
- x = order_object.price - (order_object.discount_percentage * order_object.price);
- break;
- case "sale":
- x = order_object.price - order_object.markdown;
- break;
- default:
- x = order_object.price;
- }
- // Preparing the result for the sub total purchase amount
- var subtotal_str = (order_object.price_level == "free") ? "Subtotal: This order is free" : "Subtotal: " + "$" + x;
- // Setting the Tax amount to 'y'
- y = (order.taxes_applicable == true) ? order_object.tax : 0;
- // Preparing the result for the tax amount
- var tax_str = "Tax: $" + y;
- // Here there is no need to declare variable 'y' explicitly as no calculations are performed on it, but still used it anticipating
- // future needs.
- // Preparing the result for the total order cost
- var total_str = "Order total: " + "$" + x + y;
- // Preparing the HTML result for the order
- html = "<h1> Order summary </h1>" + "<p>" + "Product: " + order_object.product_name + "</p>" + "<p>" + subtotal_str + "</p>" + "<p>" + tax_str + "</p>" + "<p>" + total_str + "</p>";
- // Displaying out the result
- document.write(html);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement