Advertisement
pranav-kural

Shopify Love the code Challenge

Jun 30th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Pranav Kural
  2. // Render HTML code for an order summary
  3. function order_summary(order_object) {
  4.  
  5. // Declaring the variables  x - cost amount, y - tax amount
  6.   var x, y;
  7.  
  8. // 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)
  9. // Using Switch Statement as it is more appropriate for the purpose of performing task based on the values of an expression.
  10.     switch (order_object.price_level) {
  11.     case "free":
  12.         x= 0;
  13.         break;
  14.     case "discount":
  15.         x =  order_object.price - (order_object.discount_percentage * order_object.price);
  16.         break;
  17.     case "sale":
  18.         x =  order_object.price - order_object.markdown;
  19.         break;
  20.     default:
  21.         x = order_object.price;
  22.     }
  23.  
  24. // Preparing the result for the sub total purchase amount
  25.     var subtotal_str = (order_object.price_level == "free") ? "Subtotal: This order is free" : "Subtotal: " + "$" + x;
  26.  
  27. // Setting the Tax amount to 'y'
  28.     y = (order.taxes_applicable == true) ? order_object.tax : 0;
  29.  
  30. // Preparing the result for the tax amount
  31.     var tax_str = "Tax: $" + y;
  32.  
  33. // Here there is no need to declare variable 'y' explicitly as no calculations are performed on it, but still used it anticipating  
  34. // future needs.
  35.  
  36. // Preparing the result for the total order cost
  37.     var total_str = "Order total: " + "$" + x + y;
  38.  
  39. // Preparing the HTML result for the order
  40.   html = "<h1> Order summary </h1>" + "<p>" + "Product:  " + order_object.product_name  + "</p>" + "<p>" + subtotal_str + "</p>" + "<p>" + tax_str + "</p>" + "<p>" + total_str + "</p>";
  41.  
  42. // Displaying out the result
  43.   document.write(html);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement