Files

45 lines
1.0 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Modify an Array Stored in an Object
---
# Modify an Array Stored in an Object
2018-10-12 15:37:13 -04:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
- The function can be written in just two lines of code.
- The first line should just use the `push()` function to add the `friend`parameter to the array found in `user.data.friend`. The second line will return the modified array.
- Remember that `user` mustb be referenced with the first parameter given to the `addFriend()` function.
---
## Solutions
2018-10-12 15:37:13 -04:00
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```javascript
2018-10-12 15:37:13 -04:00
let user = {
name: "Kenneth",
2018-10-12 15:37:13 -04:00
age: 28,
data: {
username: "kennethCodesAllDay",
joinDate: "March 26, 2016",
organization: "freeCodeCamp",
friends: ["Sam", "Kira", "Tomo"],
2018-10-12 15:37:13 -04:00
location: {
city: "San Francisco",
state: "CA",
country: "USA"
2018-10-12 15:37:13 -04:00
}
}
};
function addFriend(userObj, friend) {
// change code below this line
2018-10-12 15:37:13 -04:00
userObj.data.friends.push(friend);
return userObj.data.friends;
// change code above this line
}
console.log(addFriend(user, "Pete"));
2018-10-12 15:37:13 -04:00
```
</details>