-3

when it goes to line 21 it thinks there is no function deposit I am confuced as it should have that function. Please explain why.

1   function makeBankAccount(){
2     var bala = 0
3     function balance(b)
4     {
5       return bala;
6     }
7     function withdraw(b)
8     {
9       bala = bala - b;
10    }
11    function deposit(b)
12    {
13      bala = bala + b;
14    }
15    return makeBankAccount;
16  }
17  
18  var account1 = makeBankAccount();
19  var account2 = makeBankAccount();
20  
21  account1.deposit(5); 
22  console.log(account1.balance()); // -> 5
23  account1.withdraw(5);
24  console.log(account1.balance()); // -> 0
25  
26  account2.withdraw(5);
27  account2.withdraw(5);
28  account2.deposit(5); 
29  account2.deposit(5); 
30  account2.deposit(5);
31  account2.deposit(5);
32  console.log(account2.balance()); // -> 10
  • 1
    If you're trying to create an object that has methods with `makeBankAccount()`, you're doing it wrong. Please go find a very basic tutorial on creating an object with methods in Javascript. This isn't something we should be answering here. Here's a good place to start: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects – jfriend00 Feb 20 '16 at 04:31
  • 1
    Possible duplicate of [How do you create a method for a custom object in JavaScript?](http://stackoverflow.com/questions/504803/how-do-you-create-a-method-for-a-custom-object-in-javascript) – Alexei Levenkov Feb 20 '16 at 04:32

3 Answers3

0

This is because your function deposit(b) is not global.

deposit(b) is only accessible inside make makeBankAccount() and your objects are present outside. So deposit(b) is out of scope.

Also new keyword is missing in object creation. Change it to:

Object 1:

var account1 = new makeBankAccount();

Object 2:

var account2 = new makeBankAccount();
Mathews Mathai
  • 1,707
  • 13
  • 31
0

Two ways you can do this:

function makeBankAccount(){
    var bala = 0

    function balance(b) {
        return bala;
    }

    function withdraw(b) {
        bala = bala - b;
    }

    function deposit(b) {
        bala = bala + b;
    }
    return {
        balance: balance,
        withdraw: withdraw,
        deposit: deposit
    };
}

Then your usage code will be unchanged

or

function makeBankAccount(){
    this.bala = 0
}

makeBankAccount.prototype.balance = function(b) {
    return this.bala;
};

makeBankAccount.prototype.withdraw = function(b) {
    this.bala -= b;
};

makeBankAccount.prototype.deposit = function (b) {
    this.bala += b;
};

then your instantiate account1/account2 like

var account1 = new makeBankAccount();
var account2 = new makeBankAccount();

The rest of the code rmains unchanged

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

Try this :

var balance = 10; //You may have the balance saved anywhere else. Get it here.
var makeBankAccount = {
    amount: 0,
    balance: function () {
        return balance;
    },
    withdraw: function () {
        return balance - this.amount;
    },
    deposit: function () {
        return balance + this.amount;
    }
}
function Balance() {
  document.getElementById("Balance").innerHTML = makeBankAccount.balance();
}
function Withdraw(x) {
  makeBankAccount.amount = x;
  document.getElementById("Withdraw").innerHTML = makeBankAccount.withdraw();
}
function Deposit(x) {
  makeBankAccount.amount = x;
  document.getElementById("Deposit").innerHTML = makeBankAccount.deposit();
}
<html>
<body>
  <button onclick="Balance()">Balance</button>
  <button onclick="Withdraw(6)">Withdraw</button>
  <button onclick="Deposit(7)">Deposit</button>
  <p id="Balance"></p>
  <p id="Withdraw"></p>
  <p id="Deposit"></p>
</body>
</html>

Remaining is upto you to get the balance and pass the amount to the functions.

Munawir
  • 3,346
  • 9
  • 33
  • 51