E-commerce Product System
Part of the Object Oriented Programming section of Coddy's JavaScript journey — lesson 54 of 56.
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 returnsthis.#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
- Calculates
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
1Objects & The this Keyword
Quick Review: ObjectsAdding Methods to ObjectsUnderstanding the this KeywordConstructor FunctionsThe new KeywordRecap Challenge7 Inheritance & The extends Key
InheritanceThe "is-a" RelationshipThe extends KeywordThe super() MethodInheriting Properties&MethodsRecap Challenge2Organizing Code
What are Modules?Exporting with exportImporting with importDefault vs. Named Exports8Organizing OOP Code
Organize Classes into Modules11Project: A Shape Renderer
Setup: Shape Class & ExportCircle Class Inheritance9Static Methods & Properties
Class-Level vs. Instance-LevelStatic PropertiesStatic Utility MethodsRecap challenge12Getters & Setters
The get and set KeywordsComputed PropertiesValidation and Side EffectsRecap Challenge15 Final Challenges
Game Character BuilderE-commerce Product SystemUniversity Management SystemSmart Home Device System