CHALLENGE
class BankAccount {
#balance;
#transactionLog = [];
constructor(initialBalance) {
this.#balance = initialBalance;
}
deposit(amount) {
this.#balance += amount;
this.#transactionLog.push(`+${amount}`);
return this;
}
withdraw(amount) {
if (amount > this.#balance) throw new Error("Insufficient funds");
this.#balance -= amount;
this.#transactionLog.push(`-${amount}`);
return this;
}
get summary() {
return `Balance: ${this.#balance} | Txns: ${this.#transactionLog.join(", ")}`;
}
}
const account = new BankAccount(100);
account.deposit(50).deposit(25).withdraw(30);
console.log(account.summary);
console.log(account.hasOwnProperty("#balance"));❤2👍1
What is the output?
Anonymous Quiz
12%
true
30%
Balance: 175 | Txns: +50, +25, -30 false
34%
Balance: 145 | Txns: +50, +25, -30
23%
Balance: 145 | Txns: +50, +25, -30 false
🔥4
Please open Telegram to view this post
VIEW IN TELEGRAM
❤6
CHALLENGE
class Vehicle {
#speed = 0;
constructor(type, maxSpeed) {
this.type = type;
this.maxSpeed = maxSpeed;
}
accelerate(amount) {
this.#speed = Math.min(this.#speed + amount, this.maxSpeed);
return this;
}
getStatus() {
return `${this.type} going ${this.#speed}/${this.maxSpeed} km/h`;
}
}
class ElectricVehicle extends Vehicle {
#battery;
constructor(type, maxSpeed, battery) {
super(type, maxSpeed);
this.#battery = battery;
}
accelerate(amount) {
this.#battery -= amount * 0.5;
return super.accelerate(amount);
}
getStatus() {
return `${super.getStatus()} | Battery: ${this.#battery}%`;
}
}
const ev = new ElectricVehicle("Tesla", 250, 100);
ev.accelerate(80).accelerate(200);
console.log(ev.getStatus());
console.log(ev instanceof Vehicle);
console.log(ev.constructor === ElectricVehicle);
❤6👍1🤩1
CHALLENGE
const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);
const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x);
const double = x => x * 2;
const addTen = x => x + 10;
const square = x => x ** 2;
const negate = x => -x;
const composed = compose(negate, square, addTen, double);
const piped = pipe(negate, square, addTen, double);
const val = 3;
console.log(composed(val)); // compose: right-to-left
console.log(piped(val)); // pipe: left-to-right
❤2👍1
❤8
CHALLENGE
const company = {
name: "TechCorp",
ceo: {
name: "Morgan",
address: {
city: "Berlin"
}
},
getRevenue: () => 5_000_000
};
const cfoCity = company?.cfo?.address?.city ?? "Unknown";
const ceoCity = company?.ceo?.address?.city ?? "Unknown";
const ceoCountry = company?.ceo?.address?.country ?? "N/A";
const revenue = company?.getRevenue?.() ?? 0;
const employees = company?.getEmployees?.() ?? "No data";
console.log(cfoCity, ceoCity, ceoCountry, revenue, employees);CHALLENGE
const data = [
{ name: "Zara", score: 88 },
{ name: "Liam", score: 95 },
{ name: "Maya", score: 88 },
{ name: "Omar", score: 72 },
{ name: "Nina", score: 95 },
];
const sorted = [...data].sort((a, b) =>
b.score !== a.score
? b.score - a.score
: a.name.localeCompare(b.name)
);
console.log(sorted.map(p => `${p.name}:${p.score}`).join(", "));
❤5🤔1