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

Why this is not working? it says 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.stringGPA = stringGPA;
        this.gpa = gpa;
    }

  stringGPA(){
    const string = this.gpa.toString();
    return string;
  }

}


const student = new Student(3.9);

hey don't know why but I I try what you did and I got it right don't know why you got it wrong

Karina Machado
Karina Machado
5,848 Points

you don't need to put the stringGPA method as a parameter of the constructor method.

Ex:

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

stringGPA(){ const string = this.gpa.toString(); return string; }

}

const student = new Student(3.9);

2 Answers

rydavim
rydavim
18,765 Points

It looks like you may have gotten a bit side tracked from the challenge task. Using the code from the first task (below) you should write code inside the stringGPA method to convert the gpa value into a string. You don't need to change anything about the constructor.

class Student {
  constructor(gpa){ // leave this alone
    this.gpa = gpa;
  } 
  stringGPA(){ // this is the empty method from task 1
    // task 2 code goes here
  }
}
const student = new Student(3.9);

You'll be using this.gpa again, and you only need to write a return statement inside the stringGPA method - such that it returns the value of gpa from the constructor as a string.

Hopefully that helps, but let me know if it's still giving you trouble and we can walk through a more complete solution. Good luck, and happy coding!

Alan Villarreal, thank you so much, Alan, I had to close my browser and check again and it did work that time, it was really weird, sometime treehouse challenge has little glitches like that i guess.

yup I know I have problems that I takes long answer me if im wrong or right an it tells me that I have to restart