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

Lauren Pinckney
PLUS
Lauren Pinckney
Courses Plus Student 7,229 Points

Why am I getting error: "student.stringGPA is not a function"?

Inside the stringGPA() method, convert the value of the gpa property to a string and return it.

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

    stringGPA(){
      return toString('gpa');
    }


}

const student = new Student(3.9);

5 Answers

you should call the toString() method on the property:-

class Student {
    constructor(gpa){
        this.gpa = gpa;
    }
  stringGPA(){
    return (this.gpa).toString();
  }
}

const student = new Student(3.9);
Steven Parker
Steven Parker
210,863 Points

You won't need to add a new property for this challenge. And adding one with the same name as the method replaces the method, then it becomes "not a function". So don't change the constructor.

And "tosString" is not a plain function, but it is a method you can attach to a numeric variable using the dot notation.

I'll bet you can get it now without an explicit spoiler.

Spencer Bigum
Spencer Bigum
3,250 Points

Shouldnt it be this:

class Student {
    constructor(gpa, stringGPA){
        this.gpa = gpa;
        this.stringGPA = stringGPA;
    }

    stringGPA(){
      return this.gpa.toString();
    }


}

const student = new Student(3.9);
Steven Parker
Steven Parker
210,863 Points

No, that doesn't address the main issue. And explicit answers without any explanation are strongly discouraged by Treehouse and may be redacted by moderators.

Spencer Bigum
Spencer Bigum
3,250 Points

wow first time someone has been unhappy with free help.