25. Private Methods. Help!


#1

Hi.

Here is my code:
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;

var returnBalance = function() {
return bankBalance;
};

// create the new function here
this.askTeller = function() {
retrun bankBalance;
};
}

var john = new Person('John','Smith',30);
console.log(john.returnBalance);
var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();
console.log(myBalance);

And here is the error message I get when I try to run it:

Oops, try again. There was a problem with your syntax.

What am I doing wrong?

Thanks.


#2

Hi first in this method

// create the new function here
this.askTeller = function() {
    retrun bankBalance;
};

you wrote retrun instead of return
then these lines

var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();

you saved john.askTeller(); in myBalanceMethod so you dont need to put the () here

var myBalance = myBalanceMethod();

to myBalanceMethod


#3

Hi.

These are the changes I've made:

ion Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;

var returnBalance = function() {
return bankBalance;
};

// create the new function here
this.askTeller = function() {
return bankBalance;
};
}

var john = new Person('John','Smith',30);
console.log(john.returnBalance);
var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod
console.log(myBalance);

This is the error message I'm getting:

Oops, try again. askTeller should return the method itself. You should not have parentheses after returnBalance!

What could be the problem?

Thanks.


#4

Hi sorry I made a mistake here

var myBalance = myBalanceMethod

you should add the () to myBalanceMethod() then in the method askeTeller you should return the method returnBalance like that

this.askTeller = function()  {
    return returnBalance;
};

#5

I finally solved it. Thanks a lot!