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 Practice Classes in JavaScript Practicing Classes Practice Adding Methods

Add a method to the User class called changeUsername()

Add a method to the User class called changeUsername() This method should receive one parameter username, a string representing a new username for the user1 object. This method should not return anything Inside the method, update the value of the username property to the value of the username parameter

User.js
class User {
    constructor(email, username, birthday) {
        this.email = email;
        this.username = username;
        this.birthday = birthday;

    }

   changeUsername(username) {
        this.username = username;
        user1.changeUsername("TreehouseStudent2");

    }

}

your code is giving a RangeError: RangeError: Maximum call stack size exceeded. The right way is to create an object outside the class scope and then call its changeUserName method to update the name.

3 Answers

class User {
    constructor(email, username, birthday) {
        this.email = email;
        this.username = username;
        this.birthday = birthday;

    }

   changeUsername(username) {
        this.username = username;
    }

}

let user1 = new User('abc@hotmail.com', 'xyz' , '0000/00/00');
console.log(user1.username);
user1.changeUsername("TreehouseStudent2");
console.log(user1.username);

Thank you so much immad Ud din .

Diane Bond
Diane Bond
8,011 Points

FYI you do not need to change all the parameters; the only one that is required is username; therefore only the second to last line of code is required.

Peter Hiks
Peter Hiks
642 Points

here is the answer

class User {
    constructor(email, username, birthday) {
        this.email = email;
        this.username = username;
        this.birthday = birthday;
        }
        changeUsername(username) {
        this.username = username;


    }
}

var user1 = new User('JavaScriptStudent@teamtreehouse.com', 'JSUser1', '1/08/1991');
user1.changeUsername("username");