Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

1.0 KiB

title
title
Modify an Array Stored in an Object

Modify an Array Stored in an Object


Problem Explanation

  • The function can be written in just two lines of code.
  • The first line should just use the push() function to add the friendparameter to the array found in user.data.friend. The second line will return the modified array.
  • Remember that user mustb be referenced with the first parameter given to the addFriend() function.

Solutions

Solution 1 (Click to Show/Hide)
let user = {
  name: "Kenneth",
  age: 28,
  data: {
    username: "kennethCodesAllDay",
    joinDate: "March 26, 2016",
    organization: "freeCodeCamp",
    friends: ["Sam", "Kira", "Tomo"],
    location: {
      city: "San Francisco",
      state: "CA",
      country: "USA"
    }
  }
};

function addFriend(userObj, friend) {
  // change code below this line
  userObj.data.friends.push(friend);
  return userObj.data.friends;
  // change code above this line
}

console.log(addFriend(user, "Pete"));