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

Converting property value to string within class method is throwing an error.

I'm not sure what I'm doing wrong here. In order to convert the gpa to a string and then return it, is there a different way? I keep getting an error, saying that gpa isn't defined.

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

const student = new Student(3.9);

1 Answer

Hi!

Your JS syntax is a bit off.

You should rework your return statement.

toString is a method for a number variable object (as well as several other types of objects).

So the proper syntax would be:

return this.gpa.toString();  // Since toString is a method, you need to call it with ().
// (This will return the number as a string)

(It passes with that line of code, by the way...)

More info:

https://www.w3schools.com/jsref/jsref_tostring_number.asp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

I hope that helps.

Stay safe and happy coding!