24.) Accessing Private Variables 'return bankBalance' error


#1

So basically here is my code and my error that I get is: Oops, try again. Your getBalance function should return bankBalance;. However, every time I add a return statement it comes back as an illegal return statement usually. So am I just going about this the wrong way? Or am I missing some element I should have added in?

Link to Lesson: https://www.codecademy.com/en/courses/objects-ii/4/3?curriculum_id=506324b3a7dffd00020bf661

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   this.getBalance = function() {
      // your code should return the bankBalance
    console.log(bankBalance) 
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()
var myBalance = john.getBalance;
console.log(myBalance)


#2

illegal return? can i see a version of your code where you added the return?


#3

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   this.getBalance = function() {
      // your code should return the bankBalance
    console.log(bankBalance) 
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()
var myBalance = john.getBalance;
    return bankBalance
console.log(myBalance)

I am a very literal man so I just added right in...but now thinking about it is that even the correct way to do a return? haha I should be better than this :stuck_out_tongue:


#4

yes, you should do better :stuck_out_tongue: You should know that only function (and methods) can return things. Which means return needs to be used inside your method:

this.getBalance = function() {
      // your code should return the bankBalance
    console.log(bankBalance) 
   };

the comment even says you need to return bankbalance, yet you log it to the console


#5

Ok so as I was typing this out before your reply because I realize my fault but my boss walked in with a new project so I talked with him. Yes @stetim94 I fixed my issue xD I changed the console.log on line 9 to a return statement and got rid of my other return statement and I didn't get that same error so I will assume I am making progress I now get
Oops, try again. The variable myBalance should be set to the result of john.getBalance() Does this mean that instead of equaling myBalance to john.getBalance that I should set it too...7500?

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   this.getBalance = function() {
      // your code should return the bankBalance
    return bankBalance 
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()
var myBalance = john.getBalance;
console.log(myBalance)

#6

// create a new variable myBalance that calls getBalance()
var myBalance = john.getBalance;

calling a method requires parentheses, this should also go without explanation plus there is a comment to help you?


#7

Yes I finally got it worked out...xD Sometimes I think I just learn this stuff, get to a lesson, forget everything I just learned, and try to solve it on my own :stuck_out_tongue: Thanks for the help!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.