Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda