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

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!