This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Hamzaseo
over 8 years

25/30 Please help me

Hello Why my code is wrong

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() { return returnBalance; };

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

Answer 55fa0bb8e39efeff450005ff

0 votes

Permalink

Your object class constructor Person is incomplete, as you are using a closing-curly-bracket-} to early.

The code you have written reads like An **object class-constructor Person

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

with 2 separate extra function’s

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

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

and it should be

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() {
         return returnBalance;
   };
}

with comments…

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   // a =variable/property= bankBalance =private to the Instance=
   var bankBalance = 7500;

   // a =variable/property= returnBalance =private to the Instance=
    var returnBalance = function() {
        return bankBalance;
   };

   // a =variable/property= askTeller =publicly accessable=
   // create the new function here
   this.askTeller = function() {
         return returnBalance;
   };
}

//creating a NEW Instance of Person objects var john = new Person(‘John’,’Smith’,30);

//try to get access to john‘s =private= property returnBalance console.log(john.returnBalance);

// call john‘s =publicly= accessable askTeller Method // and -save- the Result into the global variable myBalanceMethod ( the Result is the associated =function= Value function() { return bankBalance; }; and will =publicly= accessable ) var myBalanceMethod = john.askTeller();

// call on the function associated with the variable myBalanceMethod // by using a pair of parentheses-( ) // and assign the Result into the variable myBalance var myBalance = myBalanceMethod();

//Display the associated number Value of the variable myBalance console.log(myBalance);

( as an extra…. as getting access to john’s =bankBalance= required you to do a =double execution=, you could have used the short version …. ) console.log( john.askTeller()() );

points
Submitted by Leon
over 8 years

Answer 55fa0cb586f55250e800061a

0 votes

Permalink

You could also read http://www.codecademy.com/forum_questions/5485bacc93767670a80024e8

Did you try… http://www.codecademy.com/guidance/choose

Reference

google search == the Book == javascript [your question] site:developer.mozilla.org

== discussions / opinions == javascript [your question] site:stackoverflow.com

== guidance == www.crockford.com http://javascript.crockford.com/code.html http://javascript.crockford.com/survey.html https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1

Code-Year https://www.codecademy.com/en/tracks/code-year?jump_to=4fce4211279bae0003006265

teach yourself javascript site:codecademy.com https://www.codecademy.com/en/tracks/teachyourself Meet JSON https://www.codecademy.com/courses/javascript-beginner-en-xTAfX/0/1?curriculum_id=50802b7d5225bf0200000767 Adding Event Handlers The Simple Way https://www.codecademy.com/courses/web-beginner-en-A0uwI/0/1?curriculum_id=50802b7d5225bf0200000767 JavaScript and Cookies https://www.codecademy.com/courses/javascript-beginner-en-5knNv/0/1?curriculum_id=50802b7d5225bf0200000767 Dealing with the DOM https://www.codecademy.com/courses/javascript-beginner-en-gwcYv/0/1?curriculum_id=50802b7d5225bf0200000767 JavaScript and CSS https://www.codecademy.com/courses/web-beginner-en-SEgtM/0/1?curriculum_id=50802b7d5225bf0200000767

points
Submitted by Leon
over 8 years

Answer 56084d4d51b88747e3000190

0 votes

Permalink

HI 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 () { return returnBalance; } }

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

points
Submitted by ChaloGamerZ
over 8 years