user
object contains three keys. The data
key contains five keys, one of which contains an array of friends
. From this, you can see how flexible objects are as data structures. We've started writing a function addFriend
. Finish writing it so that it takes a user
object and adds the name of the friend
argument to the array stored in user.data.friends
and returns that array.
user
object has name
, age
, and data
keys'
testString: 'assert("name" in user && "age" in user && "data" in user, "The user
object has name
, age
, and data
keys");'
- text: The addFriend
function accepts a user
object and a friend
string as arguments and adds the friend to the array of friends
in the user
object
testString: 'assert((function() { let L1 = user.data.friends.length; addFriend(user, "Sean"); let L2 = user.data.friends.length; return (L2 === L1 + 1); })(), "The addFriend
function accepts a user
object and a friend
string as arguments and adds the friend to the array of friends
in the user
object");'
- text: 'addFriend(user, "Pete")
should return ["Sam", "Kira", "Tomo", "Pete"]
'
testString: 'assert.deepEqual((function() { delete user.data.friends; user.data.friends = ["Sam", "Kira", "Tomo"]; return addFriend(user, "Pete") })(), ["Sam", "Kira", "Tomo", "Pete"], "addFriend(user, "Pete")
should return ["Sam", "Kira", "Tomo", "Pete"]
");'
```