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.data.friends;
// 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?
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.
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?