26. Passing arguments


#1

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)


#2

I don't know why I suck at this......I usually do good


#3

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

#4

oh..okay thank you :smiley:


#5

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?


#6

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


#7

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


#8

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


#9

what are block statements?


#10

Block

if (condition) {
statements1
} else {
statements2
}


#11

I know how to do those really easily


#12

btw i like your avatar :smiley: :smiley:


#13

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


#14

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