Modify an Array Stored in an Object

Modify an Array Stored in an Object
0

#1

Tell us what’s happening:
Hello, i’m stuck… i cant seem to pass this, don’t know what i’m getting wrong

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


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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

I think in the addFriend function, change user to userObj.


#3

Tip: you have to return the “friends” array.

return userObj.data.friends;


#4

Actually, when reading the exercise description, the list of friends should be returned, not the whole user object.


#5

Finish writing it so that it takes a user object and adds the name of the friend argument to the array stored in user.data.friends and returns that array


#6

Thanks guys, my code worked after i returned the userObj.data.friends . i was getting the instruction a bit confusing. Happy coding!