Search⌘ K
AI Features

Challenge: Command Pattern

Explore the command pattern by modifying a JavaScript bank account example. Learn to separate commands, receivers, and invokers to perform operations, improving your grasp of behavioral design patterns for coding interviews.

Problem statement

Study and run the code snippet below:

Node.js
class BankAccount {
constructor(amount){
this.amount = amount
}
checkAmount() {
console.log(this.amount)
}
withdrawMoney(withdrawamount) {
if(withdrawamount > this.amount){
console.log("Not enough money")
}
else{
this.amount -= withdrawamount
}
}
depositAmount(money){
this.amount += money
}
}
var account = new BankAccount(100)
account.checkAmount()
account.withdrawMoney(10)
account.checkAmount()
account.depositAmount(50)
account.checkAmount()

In the code above, you have ...