settingsAccountsettings
Menusettings

Q: Person (task with JS Class)

+2 votes

Write a JS class that represents a personal record. It has the following properties, all set from the constructor:

  • firstName
  • lastName
  • age
  • email

And a method toString(), which prints a summary of the information. See the example for formatting details.

Input:
The constructor function will receive valid parameters.

Output:
The toString()method should return a string.

Submit the class definition as is, without wrapping it in any function.

Examples:

Sample Input:

let person = new Person('Maria', 'Petrova', 22, 'mp@yahoo.com');
console.log(person);
 
Output:

Maria Petrova (age: 22, email: mp@yahoo.com)

asked in JavaScript category by user golearnweb

1 Answer

+2 votes

My solution is:

class Person {
    constructor(firstName, lastName, age, email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.email = email;
    }

    toString() {
        return `${this.firstName} ${this.lastName} (age: ${this.age}, email: ${this.email})`;
    }
}

let person = new Person('Maria', 'Petrova', 22, 'mp@yahoo.com');
console.log("" + person);

This task extends this one: https://forum.tutorials7.com/2249/get-persons-task-with-js-class

answered by user Jolie Ann
edited by user golearnweb
...