Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
How to push an object into a constructor?

So... I've been trying this for hours and hours... I just can't seem to get it right. Would really appreciate some help with this exercise.

function createClassPerson() {

// Create a Class to construct and object type Person

// Constructor must get:

// name (string) , Age (integer) , hobbies (array de strings) , friends (object array),

addFriend(nombre, edad) {

// method addFriend receives a string "name" and an INT for age, an object must be added:

// { name: name, age: age} to the friends array in the Person.

// this must not return anything.

class Person {
    constructor(name, age, hobbies, friends) {
      this.name = name,
      this.age = age,
      this.hobbies = hobbies,
      this.friends = friends
    };
}


var julian = new Person ("Julian", 24,["Airplanes", "Running"],[{name: "James", age: 25}, {name: "Reginald", age: 21,}]);


function addFriend(friendName, age) {
Person.friends.push(
    {
    name: friendName,
    age: age,
    }
    );
};

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

addFriend is meant to be method of class Person, so it should be inside it, and you need to use this.friends.push.

class Person {
  constructor(name, age, hobbies, friends) {
    this.name = name,
      this.age = age,
      this.hobbies = hobbies,
      this.friends = friends
  };

  addFriend(friendName, age) {
    this.friends.push({
      name: friendName,
      age: age,
    });
  };
}


var julian = new Person("Julian", 24, ["Airplanes", "Running"], [{
  name: "James",
  age: 25
}, {
  name: "Reginald",
  age: 21,
}]);


julian.addFriend("Jack", 24);
console.log(julian.friends)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!