Why does it return 4?

From Javascript, Basic Data Structures: Modify an Array Stored in an Object

My first solution to the problem was

function addFriend(userObj, friend) {
  // Only change code below this line
  return userObj.data.friends.push(friend);
  // Only change code above this line
}

but it return 4, which isn’t right. then I got confused and look at the solution which is:

  userObj.data.friends.push(friend);
  return userObj.data.friends;

the solution 's method return [ ‘Sam’, ‘Kira’, ‘Tomo’, ‘Pete’ ]

why does my code return 4 and not [ 'Sam', 'Kira', 'Tomo', 'Pete' ]

Learn Basic Data Structures: Modify an Array Stored in an Object | freeCodeCamp.org

Always read the documentation:

1 Like

It’s because userObj.data.friends.push(friend) return the value of the array after pushing the provided value in it. Return userObj.data.friends after pushing friend to it. I hope this helps :slightly_smiling_face:

1 Like

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