Having trouble with "Basic Data Structures: Modify an Array Stored in an Object"

Tell us what’s happening:
Why am I not adding ‘Pete’ to the user.data.friends array?

I tried to follow the hints instruction, it didn’t go well.

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) {
// Only change code below this line
[friend].push(user.data.friends);
return userObj.data.friends;
// Only change code above this line
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Modify an Array Stored in an Object

Link to the challenge:

Your return statment is navigating well to the friends array. You should do the same for your first statement and push friend into friends.

1 Like

Now it works, thanks for helping.

userObj is representing user so you dont need to repeat. All you have to do is push friend, nothing else

1 Like

Tested that and it works now.
Thanks for the help.