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!
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)