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

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

#1

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

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

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: [
      'Sam',
      'Kira',
      'Tomo'
    ],
    location: {
      city: 'San Francisco',
      state: 'CA',
      country: 'USA'
    }
  }
};

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


#2

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


#3

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


#4

Thanks i have corrected it now.


#5

@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?


#6

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:

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

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: