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.