Question on Basic Data Structures: Modify an Array Stored in an Object

I reviewed several of the forum posts as I have been having issues with this challenge. At first, I did this:


My thought process is “I need to get into the users section (Kenneth) then down into the “data” section and finally end up in “friends” where I would push (add) the new friend name Pete.” Then I remembered I needed to return it after I re-read the instructions. So I did this:

return  users['data']['friends'].push(friend);

That didn’t seem to work and that’s when I noticed almost everyone using dot notation in the forum help posts. So I thought maybe I shouldn’t use bracket notation in this instance. But then, based on reading the different hints from the more experienced commenters, I was still doing it wrong so I updated it to this first:

return  userObj['data']['friends'].push(friend);

When it still didn’t work, I finally switched over to dot notation. Maybe everyone was using it because that was the way the instructions were set up and I had initially overlooked that part? When I switched over to dot notation, my answer still gives me the number 4 instead of the four expected names. I’m also (obviously) not passing the last test on this challenge. Here’s what I have so far:

let user = {
  name: 'Kenneth',
  age: 28,
  data: {
    username: 'kennethCodesAllDay',
    joinDate: 'March 26, 2016',
    organization: 'freeCodeCamp',
    friends: [
    location: {
      city: 'San Francisco',
      state: 'CA',
      country: 'USA'

function addFriend(userObj, friend) {
  // change code below this line  
  // change code above this line

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

I’m not exactly sure which part is making it choose a number. Am I calling the wrong part in my return line? Or am I supposed to add another line? I’m not totally sure where I’m going wrong here. I will happily take any advice to understand better.

When you return this, the function returns the length of the array that was referenced by, while challenge asks for array friends along with friend inside it.
Try solving now.

Actually, that is not correct. It returns the length of the array referenced by after the friend is added.

Thanks i have corrected it now.

1 Like

@aditya_p and @RandellDawson Thank you for helping with the hint. However, I’m still having issues.

So the push method is what is giving me the length of 4 in the test result (based on MDN’s array.prototype.push( ) article) but I need it for this challenge, right? I mean, since the .push() adds the new friend to the list, it would make sense to keep it.

But I’m just stuck now with the code I have. I change pieces here and there and I end up worse than before. Is there a better MDN article I should review that will help with this particular challenge?

Oh my gosh. It’s such a simple solution but I finally realized it. I wasn’t fully thinking it through. I think I was over-complicating things (I do that sometimes).

My solution:;

I was so close! :laughing:

I just had to remove the .push() and create a return. Thank you both for not responding right away - it helped me to keep playing around with it. But now I can do it! :smiley: