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

Yvette Deale
Yvette Deale
19,119 Points

returning a tring value from method

In the OOJ Challenge task the question is: Inside the stringGPA() method, convert the value of the gpa property to a string and return it.

I did:

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

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

const student = new Student(3.9);

I get an error message that stringGPA is not returning a string

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

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

const student = new Student(3.9);
student.stringGPA();

2 Answers

Steven Parker
Steven Parker
229,005 Points

You need to put the keyword "return" in front of the value you wish to return.

And it doesn't cause an error, but you don't need the parentheses around "this.gpa".

Ana María Benites Rodríguez
Ana María Benites Rodríguez
1,011 Points

why we don't need to pass as an argument 'gpa' to the stringGPA() method?

Steven Parker
Steven Parker
229,005 Points

It doesn't need an argument because it uses the "gpa" property that was stored in the object when it was created by the constructor.