Help with Basic Data structures

Can someone tell me why this code is not working please? It’s supposed to add a forth element to the ‘freinds’ array.


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(user, friend) {
  // change code below this line  
    user.data.friends.push(friend);
    return user;
  // change code above this line
}

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

Hi @hmahad

You should re-check the last user story in that challenge, you may want to think about returning something else from the addFriend function.

3 Likes

it worked when i tried it out using your function(see below)
function addFriend(user, friend) {
// change code below this line
//user.data.friends.push(friend);
user.data.friends.push(friend);
return user;
// change code above this line
}

console.log(addFriend(user, ‘Pete’));

type this to check it out:
user.data.friends

I guess the return had to be ‘user.data.friends’. Thx for the help guys.

1 Like