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

How do I convert the Gpa value to string anybody?

How do I convert a numerical value to a string value in Javascript and then return it?

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

}
const student = new Student(3.9);

2 Answers

Andreas Nyström
Andreas Nyström
8,879 Points

The easiest way to create a string is to add an empty string to it.

  stringGPA(){
    // will return the gpa as a string.
    return this.gpa + "";
  }

Could also mention to make a number out of a string, (the string has to hold a number, but as a string variable), the easiest way is probably to divide it or multiply it by 1

let stringNum = "22"
// will console.log 22 as a number.
console.log(stringNum / 1)
Tim Knight
Tim Knight
28,863 Points

These certainly work Andreas but often just using toString() can be clearer for other developers looking at your code. To then convert to a number from a string you have parseInt(), number(), and parseFloat() which can all address versions of this feature without having to do equation tricks.

Andreas Nyström
Andreas Nyström
8,879 Points

Tim Knight I've yet to see someone who prefers to use these methods over the one I mentioned. But I'm glad we're different. Let me try to explain why I prefer these methods so people get both versions.

toString() does exactly + "", but doesn't work on numbers directly. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

const num = 11
// Works
num.toString()
// Doesn't work.
11.toString() 
// Basically does the same thing, except float allows decimals. Still * 1 || / 1 is faster and shorter.
parseInt(),  parseFloat(), Number()
// Note that these takes an argument like Number('11') === 11
// While toString() is called upon an object. object.toString()
Tim Knight
Tim Knight
28,863 Points

Hi Fabian,

You're on the right track with the use of toString(), however remember it's a function and needs () at the end. Additionally, since you're passing in the GPA when you initialize the class there's really no reason to pass it in again, you can just reference the GPA value from the class (which is this). Try modifying your class to look something like this.

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

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

Now calling the stringGPA() function on the instance of your class will return the value of the GPA as a string.