JSFiddle - React, Tailwind, and code Playground

by Bahman ParsaManesh

JavaScript

// jshint esnext: true
function Checking(amount) {
 this.balance = amount;
 this.deposit = function(amount) {
   this.balance += amount;
 }
 this.withdraw = withdraw;
 this.toString = toString;
}

function withdraw(amount) {
 if (amount <= this.balance) {
 this.balance -= amount;
 }
 if (amount > this.balance) {
 console.log("Insufficient funds");
 }
}
function toString() {
 return "Balance: " + this.balance;
}
var account = new Checking(500);
account.deposit(1000);
console.log(account.toString()); // Balance: 1500
account.withdraw(750);
console.log(account.toString()); // Balance: 750
account.withdraw(800); // displays "Insufficient funds"
console.log(account.toString()); // Balance: 750