Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,879 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 :).