So in this particular exercise we are given an user obj as below;
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' } } };
Exercise
Write a function that adds a new friend to friends array in the user obj
They provide us the following function structure
function addFriend(userObj, friend) { // Only change code below this line // Only change code above this line } console.log(addFriend(user, 'Peter Gregory'));
Correct Answer? Answer 2
In attempt 1 we are returning what push function returns. Push returns the length of the array , instead of the array. Therefore we have to push the new friend and then return the array in the next line separately
Once you add Peter Gregory as a friend, remember to get him some asparagus!
Check the exercise here https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/modify-an-array-stored-in-an-object
Top comments (0)