Programming with JavaScript

⌘K
  1. Home
  2. Docs
  3. Programming with JavaScri...
  4. Practice Programs
  5. Enhanced Object Literal for Online Store

Enhanced Object Literal for Online Store

Example: 6

Illustration of Enhanced Object Literal

In the context of an online store application, an `onlineStore` object has been created with specific functionalities:

  • The `addProduct` method enables the addition of products to the shopping cart. It requires two arguments: `productName` and `productPrice`.
  • The `calculateSubtotal` method computes the total cost of all products in the shopping cart.
  • The `applyCoupon` method applies discounts to the total based on provided coupon codes. Valid codes include `SALE10` for a 10% discount, `SPECIAL20` for a 20% discount, and `HALFPRICE` for a 50% discount. In case of an invalid code, it returns an “Invalid coupon code” message.

Three products have been added to the cart: a laptop priced at $800, headphones priced at $100, and a smartphone priced at $600. The objective is to apply the “SPECIAL20” coupon code and determine the discounted total.

  1. Compute and present the subtotal of the shopping cart before any coupon is applied.
  2. Implement the “SPECIAL20” coupon code and reveal the final cost after applying the discount.

In the event that an invalid coupon code is provided, display the message “Invalid coupon code.”

Script

const onlineStore = {
  products: [],

  addProduct(productName, productPrice) {
    const product = { name: productName, price: productPrice };
    this.products.push(product);
  },

  calculateSubtotal() {
    let subtotal = 0;
    for (const product of this.products) {
      subtotal += product.price;
    }
    return subtotal;
  },

  applyCoupon(code) {
    const couponCodes = {
      SALE10: 10, // 10% discount
      SPECIAL20: 20, // 20% discount
      HALFPRICE: 50, // 50% discount
    };

    if (couponCodes.hasOwnProperty(code)) {
      const discountPercentage = couponCodes[code];
      console.log(code);
      console.log(couponCodes[code]);
      const subtotal = this.calculateSubtotal();
      const discountAmount = (discountPercentage / 100) * subtotal;
      const discountedTotal = subtotal - discountAmount;
      return discountedTotal;
    } else {
      return "Invalid coupon code";
    }
  },
};

onlineStore.addProduct("Laptop", 800);
onlineStore.addProduct("Headphones", 100);
onlineStore.addProduct("Smartphone", 600);

const couponCode = "SPECIAL20";
const discountedTotal = onlineStore.applyCoupon(couponCode);

console.log("Subtotal before coupon:", onlineStore.calculateSubtotal());
if (typeof discountedTotal === "number") {
  console.log(
    `Total cost after applying ${couponCode} coupon:`,
    discountedTotal
  );
} else {
  console.log(discountedTotal); // Invalid coupon code message
}

Output

Loading

Views: 31

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments