Day 3 — Operators, coercion, and logic
Homework
Task 1 GST calculator (no functions)
- Use
const price = 1500andconst gstRate = 18. - Compute GST amount and total (price + GST). Do not wrap this in a function — plain top-level variables and expressions.
- Log price, GST amount, and total.
const price = 1500;
const gstRate = 18;
const gstAmount = (price * gstRate) / 100;
const total = price + gstAmount;
console.log("Price:", price);
console.log("GST Amount:", gstAmount);
console.log("Total Amount:", total);
Reference: ../js/day3.js. Run with node day3.js from day-3/js.
Task 2
== vs === — ten pairs + comment table
- Compare 10 different value pairs with both
==and===(log each pair). - In a block comment, draw a small table summarising which pairs differ between
==and===and why (coercion vs strict types).
/*
| # | Expression A | Expression B | == | === | Why (short) |
|---|----------------|--------------|-------|-------|--------------------------------------|
| 1 | 5 | "5" | true | false | == converts string to number |
| 2 | true | 1 | true | false | == converts boolean to number |
| 3 | false | 0 | true | false | ==: false → 0 |
| 4 | null | undefined | true | false | == special case; types differ |
| 5 | "" | 0 | true | false | ==: both treated as “falsy number” |
| 6 | " " | 0 | true | false | ==: string trims/coerces to number |
| 7 | [] | false | true | false | ==: [] → "" → 0 |
| 8 | [1] | 1 | true | false | ==: array coerced to primitive |
| 9 | 0 | false | true | false | ==: false → 0 |
|10 | "10" | 10 | true | false | ==: string "10" → number 10 |
| | | | | | === never coerces — types must match |
*/
console.log(5 == "5", 5 === "5");
console.log(true == 1, true === 1);
console.log(false == 0, false === 0);
console.log(null == undefined, null === undefined);
console.log("" == 0, "" === 0);
console.log(" " == 0, " " === 0);
console.log([] == false, [] === false);
console.log([1] == 1, [1] === 1);
console.log(0 == false, 0 === false);
console.log("10" == 10, "10" === 10);
Full table + all ten pairs: ../js/day3.js.
Task 3
Ternary: "adult" vs "minor"
- Write a ternary:
age >= 18 ? "adult" : "minor". - Test with five different ages (five separate
console.loglines).
console.log(10, "→", 10 >= 18 ? "adult" : "minor");
console.log(17, "→", 17 >= 18 ? "adult" : "minor");
console.log(18, "→", 18 >= 18 ? "adult" : "minor");
console.log(25, "→", 25 >= 18 ? "adult" : "minor");
console.log(65, "→", 65 >= 18 ? "adult" : "minor");
Reference: ../js/day3.js.
Task 4
Discount calculator (&& + ternary)
- If the user is a member and purchase total > 1000, apply a 10% discount; otherwise discount is 0.
- Use
&&together with a ternary (no separate function required). - Log discount and final amount. Try at least: member + high total, member + low total, non-member + high total.
let isMember = true;
let purchaseTotal = 1500;
let discount = isMember && purchaseTotal > 1000 ? purchaseTotal * 0.1 : 0;
let finalAmount = purchaseTotal - discount;
console.log("Case A → discount:", discount, "final:", finalAmount);
isMember = true;
purchaseTotal = 800;
discount = isMember && purchaseTotal > 1000 ? purchaseTotal * 0.1 : 0;
finalAmount = purchaseTotal - discount;
console.log("Case B → discount:", discount, "final:", finalAmount);
isMember = false;
purchaseTotal = 1500;
discount = isMember && purchaseTotal > 1000 ? purchaseTotal * 0.1 : 0;
finalAmount = purchaseTotal - discount;
console.log("Case C → discount:", discount, "final:", finalAmount);
Reference: ../js/day3.js.