26. Passing arguments

I looked at the hint and followed the syntax but it says it’s wrong…here’s my code:

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

this.askTeller = function(pass) {
if (pass == 1234) return bankBalance;
else return “Wrong password.”;
};
}

var john = new Person(‘John’,‘Smith’,30);
/* the variable myBalance should access askTeller()
with a password as an argument */
var myBalance = askTeller.function(1234)

I don’t know why I suck at this…I usually do good

the askTeller function is bound to the Person class and therefore each Person Object created. To invoke the function you need to use the specific instance like this

var myBalance = john.askTeller(1234);
console.log(myBalance);
2 Likes

oh…okay thank you :smiley:

but why print out myBalance? is it because when you assign the variable to the john askTeller and when you print it, it outputs the balance?

1 Like

in the function implementation you are returning the person’s balance so to see the result on the console you need print it there

oh! because it’s the balance of john so you want to use john.askTeller and out in the correct password to get the balance

Hi,

you have an error here:

var myBalance = askTeller.function(1234)

You made a new Object called John here.

var john = new Person('John','Smith',30);

So you should call askTeller like this

john.askTeller(1234);

And please it is a good practice to always use block statements

1 Like

what are block statements?

Block

if (condition) {
statements1
} else {
statements2
}

I know how to do those really easily

btw i like your avatar :smiley: :smiley:

Thanks bro :smiley: I like yours too :slight_smile:

1 Like

storm5638,

This code worked really well 4 me:

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

this.askTeller = function(pass) {
if (pass == 1234) return bankBalance;
else return “Wrong password.”;
};
}

var john = new Person(‘John’,‘Smith’,30);
/* the variable myBalance should access askTeller()
with a password as an argument */
var myBalance = john.askTeller(1234);
console.log(myBalance);