Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

JS exercise bank account constructor functions and prototypes solution

function Account(name, balance) {
  this.name = name;
  this.balance = balance;
}

Account.prototype.deposit = function(amount) {
  if (this._isPositive(amount)) {
    this.balance += amount;
    console.info(`Deposit: ${this.name} new balance is ${this.balance}`);
    return true;
  }
  return false;
}

Account.prototype.withdraw = function(amount) {
  if (this._isAllowed(amount)) {
    this.balance -= amount;
    console.info(`Withdraw: ${this.name} new balance is ${this.balance}`);
    return true;
  }
  return false;
}

Account.prototype.transfer = function(amount, account) {
  if (this.withdraw(amount) && account.deposit(amount)) {
    console.info(`Transfer: ${amount} has been moved from ${this.name} to ${account.name}`);
    return true;
  }
  return false;
}


Account.prototype._isPositive = function(amount) {
  const isPositive = amount > 0;
  if (!isPositive) {
    console.error('Amount must be positive!');
    return false;
  }
  return true;
}

Account.prototype._isAllowed = function(amount) {
  if (!this._isPositive(amount)) return false;

  const isAllowed = this.balance - amount >= 0;
  if (!isAllowed) {
    console.error('You have insufficent funds!');
    return false;
  }
  return true;
}

const a = new Account('a', 100);
const b = new Account('b', 0);


output.innerText += `before:  a: ${a.balance}, b: ${b.balance}
`;

a.transfer(100, b);

output.innerText += `after:  a: ${a.balance}, b: ${b.balance}
`;
Comment

PREVIOUS NEXT
Code Example
Javascript :: react addon update 
Javascript :: When you run JavaScript in a Node.Js application, elements in a Node.JS Stack actually executes the JavaScript: 
Javascript :: locak storage for table js 
Javascript :: this.setState is undefined inside a async function in js 
Javascript :: flowjs attributes 
Javascript :: javascript Arranging Coins 
Javascript :: parent folder of file extendscript 
Javascript :: javascript program german to english translation 
Javascript :: $(document).ready(function() { $(".menu-icon").on("click", function() { $("nav ul").toggleClass("showing"); }); }); 
Javascript :: javascript es6 dom manipulation 
Javascript :: adding a variable to a string without using + in javascript 
Javascript :: formatDoubl js 
Javascript :: vue change input value from console 
Javascript :: passing the href in ajax call 
Javascript :: how enable custom css and js vscode ubuntu 
Javascript :: react redux cheat sheet 
Javascript :: react conditional if localhost 
Javascript :: using shortid in react lists 
Javascript :: npm run coverage is throwing some error 
Javascript :: how to change grid size with conditional expression in react 
Javascript :: function calls 
Javascript :: Checkbox not binding to scope in angularjs 
Javascript :: mat slider in a reactve form 
Javascript :: september 
Javascript :: toastr js dont fade out 
Javascript :: vue slot events 
Javascript :: how to get header in all the pages in visualforce page 
Javascript :: recorrer letra por letra js 
Javascript :: JavaScript Implementation of Linear Search 
Javascript :: Iterating through document elements using for each in javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =