26. passing arguments


#1
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();
myBalance(1234);

If i run this code it returns this error:
SyntaxError: Unexpected token (

Any ideas as to what went wrong?


#2

Running it as an HTML document returns the same result.


#3

var myBalance() = john.askTeller();

"myBalance" is a variable however it doesn't need to have parameters because it itself isn't being called a function. I believe if you delete the parenthesis attached to myBalance it'll clear the error. Otherwise your code is very professionally done.


#4
    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);
myBalance;

That's the change i needed to make. Thanks!


#5

I'm confused, I entered this code and I get the bankBalance '7500' in the console but it doesn't pass? What am I doing wrong?

My Code

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

myBalance(1234);

Also passes, but doesn't seem right

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

var myBalance = john.askTeller(1234);

Correct Answer

var myBalance = john.askTeller(1234);

myBalance;


#6

Now, why wouldn't it work if you would call it like this
var john = new Person('John','Smith',30);
/* the variable myBalance should access askTeller()
with a password as an argument */
var myBalance = john.askTeller(pass);

I mean, if in the function pass = 1234, shouldn't it be ok for it to be:
var myBalance = john.askTeller(pass)?