-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
2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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();

2 Comments

But isnt the account2 an instance of makebankaccount?
no it is not ... no new keyword, and makebankaccount has no (new) methods on it's prototype

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.