i am Trying to add new item to the Friends property of users Object, but it isn’t working. Please how do i handle this. i have tried different approach but still not getting the right answer.
Your code 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) {
// Only change code below this line
let newFriend = userObj.data.friends.push(friend);
console.log(newFriend);
//let answer = newFriend.push(friend);
//console.log(answer)
return newFriend;
// Only change code above this line
}
console.log(addFriend(user, 'Pete'));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36.
Hi @CodebanK,
You can add an item to an array in multiple ways, but the most common way is to use the “push()” function; like “push(‘CodeBanK’);”
To add to this particular array, I would “drill down to it” by accessing it with “dot notation”: “user.data.friends”.
The complete instruction would be:
user.data.friends.push(‘CodeBanK’);
I hope that this helps. You might find researching the following to be useful:
push(), shift(), dot notation, and bracket notation.
You have successfully added a new friend to the array. So that’s not your problem. The problem is your return statement. Your task is to return the modified array.
let newFriend // delete this
userObj.data.friends.push(friend); // keep this
return newFriend; // change to modified array
You are really close you just have to return the right part.
Hope that helps!