Search⌘ K

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 a BankAccount class. You can check the amount in the account using the checkAccount function, withdraw a certain amount using the withdrawMoney function, and deposit an amount using the depositAmount function. ...