Modify an Array Stored in an Object-help

Modify an Array Stored in an Object-help
0

#1

Tell us what’s happening:
whats wrong in this?

Your code so far


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
  let userFriend = userObj.data.friends + ',' +  friend;
  return userFriend; 
  // change code above this line
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/modify-an-array-stored-in-an-object


#2

Hi,

You kind of have the right idea, but you are treating friends (user.data.friends) as a string rather than an array.

What you need to do is push the new friend onto the end of the array.

Hope this helps :slight_smile:

(Hint: is there an array method called push?)