Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript Working with Classes in JavaScript Adding methods to classes

But: Bummer: gpa is not defined.

Does anybody know what is wrong with gpa? Without me getting annoid by trying 1000 times over

adding_methods.js
class Student {
    constructor(gpa){
        this.gpa = gpa;
    }
    stringGPA()
  {
    return gpa.valueOf(gpa);
  }
}

const student = new Student(3.9);

1 Answer

Andreas Nyström
Andreas Nyström
8,887 Points

Hi. You're inside a class. So you need to refer to the gpa with this.gpa.

class Student {
    constructor(gpa){
        this.gpa = gpa;
    }
    stringGPA()
  {
    // gpa here should be this.gpa. This way you refer to the instance of the class. 
    return gpa.valueOf(gpa);
  }
}

const student = new Student(3.9);

Also note that you probably want to return a string, valueOf doesn't really do that. Try figuring out what method to use for this or reply here and I'll help you. But for now you need to refer to gpa as this:

stringGPA(){
  // Note: this is not what you want, but just showing you how to refer to gpa.
  return this.gpa.valueOf(this.gpa)
}

If you got any more questions just write here :).