settingsAccountsettings
Menusettings

Q: Get Persons (task with JS Class)

+3 votes

Write a JS function that returns an array of Person objects. Use the class from the previous task: https://forum.tutorials7.com/2247/person-task-with-js-class, create the following instances, and return them in an array:

task with js class

For any empty cells, do not supply a parameter (call the constructor with less parameters).

Input / Output

There will be no input, the data is static and matches the table above. As output, return an array with Person instances.

asked in JavaScript category by user Jolie Ann
edited by user golearnweb

1 Answer

+2 votes

My solution:

function getPersons() {
    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})`;
        }
    }

    return [
        new Person("Maria", "Petrova", 22, "mp@yahoo.com"),
        new Person("SoftUni"),
        new Person("Stephan", "Nikolov", 25),
        new Person("Peter", "Kolev", 24, "ptr@gmail.com")
    ];
}

console.log("" + getPersons());
answered by user nikole
edited by user golearnweb
...