Menu
Coddy logo textTech

E-commerce Product System

Part of the Object Oriented Programming section of Coddy's JavaScript journey — lesson 54 of 56.

challenge icon

Challenge

This is an online store product system. Products have prices, and physical products (like laptops) calculate shipping based on their weight. Digital products (like ebooks) would have free shipping.

Your task is to complete the missing methods in the product system:

1: Complete the Product class in product.js:

  • Add method getPrice() that returns this.#price
  • Add method getDescription() that returns ${this.name} - ${this.#price}

2: Complete the PhysicalProduct class in physicalProduct.js:

  • Add method calculateShipping() that:
    • Calculates this.weight * 0.5 (50 cents per pound)
    • Returns the shipping cost

Try it yourself

import { Product } from './product.js';
import { PhysicalProduct } from './physicalProduct.js';

const book = new Product('Book', 20);
const laptop = new PhysicalProduct('Laptop', 1000, 5);

console.log(book.getDescription()); // Should show: "Book - $20"
console.log(laptop.getDescription()); // Should show: "Laptop - $1000  (5 lbs)"

console.log(`Shipping cost for laptop: $${laptop.calculateShipping()}`); // Should show: "Shipping cost for laptop: $2.5"

console.log(`Book price: $${book.getPrice()}`); // Should show: "20"
console.log(`Laptop price: $${laptop.getPrice()}`); // Should show: "1000"

All lessons in Object Oriented Programming