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

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
231,268 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
231,268 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.