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

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
210,863 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,010 Points

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

Steven Parker
Steven Parker
210,863 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.