Modify an Array Stored in an Object4634

Modify an Array Stored in an Object4634
0

#1

What is the userObj bit in this answer? I’ve never written an object that way where I put the name of the key followed by ‘Obj’. I had to look up the answer for this one because there was no way I could have known to type it as ‘userObj’. This isn’t defined anywhere and nowhere else was I told to write it this way. I was told in previous lessons to type it as key.subthing.subthing2 or key[subthing][subthing2].

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  
  userObj.data.friends.push(friend);
  return userObj.data.friends;
  // 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/69.0.3497.100 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

It’s just the name of a variable. It could be called elephantPants and it would work the same.


#3

userObj is only the name of the parameter of the function, it actually can be called anything.

When the code calls the function like that: addFriend(user, 'Pete'), the function parameters receives those values, so:

  1. userObj receives the value of user
  2. friend receives the value of ‘Pete’

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/passing-values-to-functions-with-arguments/