Basic Data Structures - Modify an Array Stored in an Object

Tell us what’s happening:
Why doesn’t this work why can i not return userObj in the function?
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) {
  // Only change code below this line
  userObj.data.friends.push(friend);
  return userObj;
  // Only 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/117.0.0.0 Safari/537.36 Edg/117.0.2045.43

Challenge: Basic Data Structures - Modify an Array Stored in an Object

Link to the challenge:

Hi,

return sends the whole object in your solution, but you just want the new array.

Hint: The new array still has the old name, it was just modified.

There is a one-line solution, which produces the same result but doesn’t pass the tests for some reason:
return userObjc.data.friends.concat([friend])

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.