📅 Day 5 - Math, Date, Classes, Constructor, Inheritance

"From Calculators to Robots – Building a QA Team with OOP"

Theme: QA Shop Architecture

🕒 Session Flow

🎭 Intro Story (5 min)

🛍️ "Imagine we're setting up a virtual QA shop. We need calculators for discounts, calendars for delivery dates, workers (objects) to do jobs, and managers (classes) to control them. Some workers have special roles (inheritance). Today, we'll become the architects of this QA shop."

1. 🧮 Math Object (10 min)

📌 Concept

Built-in object for calculations.

📌 Story tie-in

QA Shop needs a calculator for prices, discounts, random IDs.

console.log(Math.round(4.7)); // 5 console.log(Math.floor(4.9)); // 4 console.log(Math.ceil(4.1)); // 5 console.log(Math.max(100, 450, 999)); // 999 console.log(Math.random()); // random number between 0-1

🛒 QA Example: Generate random Order ID

let orderId = Math.floor(Math.random() * 10000); console.log("Order ID:", orderId);

💡 Interactive Q:

👉 If Math.random() returns 0.3456, what is the range of Math.floor(Math.random() * 10)?

Answer: Range is 0 to 9 (inclusive)

🎯 Mini Task:

  • Generate a random 5-digit coupon code.
  • Calculate max price among [100, 450, 300, 999].

2. 📅 Date Object (10 min)

📌 Concept

JS object to work with dates/times.

📌 Story tie-in

QA Shop needs a calendar to check order vs delivery date.

let today = new Date(); console.log("Today:", today); console.log("Year:", today.getFullYear()); console.log("Month:", today.getMonth()+1); // 0-based console.log("Date:", today.getDate());

🛒 QA Example: Validate if delivery date > order date

let orderDate = new Date("2025-08-19"); let deliveryDate = new Date("2025-08-25"); console.log(deliveryDate > orderDate ? "Valid Delivery" : "Invalid Delivery");

💡 Interactive Q:

👉 What happens if you create new Date("2025-13-01")?

Answer: Invalid date - month 13 doesn't exist!

🎯 Mini Task:

  • Print today's date in DD/MM/YYYY format.
  • Write isWeekend(date) → return true if Sat/Sun.

3. 🏗️ Classes & Objects (10 min)

📌 Concept

Class = blueprint, Object = instance.

📌 Story tie-in

In QA Shop, we hire workers (objects) using a job description (class).

class Product { constructor(name, price) { this.name = name; this.price = price; } getDetails() { return ` return this.name + " costs $" + this.price`; } } let laptop = new Product("Laptop", 1000); console.log(laptop.getDetails());

🛒 QA Example: Product card in OpenCart

Each product card in OpenCart can be represented as an object.

💡 Interactive Q:

👉 What happens if you call laptop.discount() when no such method exists?

Answer: TypeError: laptop.discount is not a function

🎯 Mini Task:

  • Create User class with username, password and method login().
  • Create demo user and call login().

4. 🔧 The Constructor Story (10 min)

📌 Concept

Special method in class → initializes object properties.

📖 Funny Story (QA Shop)

"When a new QA joins the shop, HR (constructor) sets up their desk: username, role, laptop. If HR forgets, they just enter as a blank worker."

"Default constructor = worker comes in with no setup, still works but no details."

"Custom constructor = worker gets proper badge, system login, role defined."

// Without constructor class Worker { greet() { console.log("Hello!"); } } let w = new Worker(); w.greet(); // works fine (default constructor exists) // With constructor class Worker { constructor(name, role) { this.name = name; this.role = role; } greet() { console.log(`Hi, I am ${this.name}, a ${this.role}`); } } let qa = new Worker("Anirudh", "QA Engineer"); qa.greet();

💡 Interactive Q:

👉 If you don't call super() in child class constructor, what happens?

Answer: ReferenceError: Must call super constructor in derived class

🎯 Mini Task:

  • Create Product with constructor(name, price).
  • Create object Laptop → 1000. Print details.

5. 🧬 Inheritance (15 min)

📌 Concept

One class extends another, reusing logic.

📌 Story tie-in

QA Shop has workers → some are Admin QAs, others Automation QAs.

class User { constructor(username) { this.username = username; } viewProducts() { console.log(`${this.username} is browsing products`); } } class Customer extends User { addToCart(product) { console.log(`${this.username} added ${product} to cart`); } } class Admin extends User { manageOrders() { console.log(`${this.username} is managing orders`); } } let c1 = new Customer("Tester_Cust"); c1.viewProducts(); c1.addToCart("Phone"); let a1 = new Admin("Tester_Admin"); a1.viewProducts(); a1.manageOrders();

🛒 QA Example:

  • Base user actions are reused.
  • Customer-specific & Admin-specific actions extend naturally.

💡 Interactive Q:

👉 What error if you try c1.manageOrders()?

Answer: TypeError: c1.manageOrders is not a function

🎯 Mini Task:

  • Create BasePage with open(url) method.
  • Extend CartPage with addProduct().
  • Use object to open site + add product.

🎯 Day 4 Learning Outcomes

📑 Day 4 Homework

  1. Generate random 6-digit OTP using Math.random().
  2. Write daysBetween(date1, date2) → returns number of days between 2 dates.
  3. Create Product class with applyDiscount(percent) method.
  4. Create ElectronicProduct extending Product → adds warranty.
  5. Create BasePage with open(). Extend LoginPage with login(). Call in main.js.