Basic Data Structures: Modify an Array Stored in an Object - question about 'Obj'

Hi all

So I’ve completed this problem below

let user = {
  name: 'Kenneth',
  age: 28,
  data: {
    username: 'kennethCodesAllDay',
    joinDate: 'March 26, 2016',
    organization: 'freeCodeCamp',
    friends: [
    location: {
      city: 'San Francisco',
      state: 'CA',
      country: 'USA'

function addFriend(userObj, friend) {
  // change code below this line;
  // change code above this line

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

This has come up as correct when I run the test so I’ve passed the lesson. What I’m wondering is, when I look at the solution for the task it shows the following as the correct solution:;

I may have missed this previously, but what differance does adding the ‘Obj’ to the code do? does this mean my solution is incorrect if it was used outside of FCC?

Thanks all

It is the difference between making the code reusable or not. user is the global object, userObj is the function parameter

If you use the function parameter the code will work with any object passed in as argument. It will not if you use the global object.

1 Like

userObj is the parameter that was passed to the addFriend function, whereas user is the global object. If this were outside the FCC tests, your function would only work on that one user global object, rather than whatever object you passed in to the function.

The tests could devise a way to test whether you were mutating the global object or the parameter, but they’re not that sophisticated, so you got away with it.

1 Like

Ahh yes, I suppose in the confines of FCC, i forget that in reality there would be multiple objects and arrays etc.

Just to confirm I’m understanding everything then. If I had another object that was similar to the one above but called user2, I could do the following:

console.log(addFriend(user2, ‘Bill’));

and that would use the same function, but alter a different user object with a different name being added?

Thanks for the quick replies, always appreciated!

Exactly correct, you got it :slight_smile:

Great! Glad it’s all starting to make sense!