Skip to content

Commit dd8d49c

Browse files
committed
Basic Data Structures (ex. 19 to 20)
+ Finished Basic Data Structures module
1 parent b53eb8a commit dd8d49c

File tree

4 files changed

+101
-21
lines changed

4 files changed

+101
-21
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Generate an Array of All Object Keys with Object.keys():
3+
Finish writing the getArrayOfUsers function so that it returns an array containing all the properties in the object it
4+
receives as an argument.
5+
6+
- The users object should only contain the keys Alan, Jeff, Sarah, and Ryan
7+
- The getArrayOfUsers function should return an array which contains all the keys in the users object
8+
*/
9+
let users = {
10+
Alan: {
11+
age: 27,
12+
online: false
13+
},
14+
Jeff: {
15+
age: 32,
16+
online: true
17+
},
18+
Sarah: {
19+
age: 48,
20+
online: false
21+
},
22+
Ryan: {
23+
age: 19,
24+
online: true
25+
}
26+
};
27+
28+
function getArrayOfUsers(obj) {
29+
// Only changed code below this line
30+
let users = [];
31+
for (let user in obj) {
32+
users.push(user);
33+
}
34+
return users;
35+
// Only changed code above this line
36+
}
37+
38+
console.log(getArrayOfUsers(users));
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Modify an Array Stored in an Object:
3+
Take a look at the object we've provided in the code editor.
4+
The user object contains three keys. The data key contains five keys, one of which contains an array of friends.
5+
From this, you can see how flexible objects are as data structures. We've started writing a function addFriend.
6+
Finish writing it so that it takes a user object and adds the name of the friend argument to the array stored in
7+
user.data.friends and returns that array.
8+
9+
- The user object should have name, age, and data keys.
10+
- The addFriend function should accept a user object and a friend string as arguments and add the friend to the array
11+
of friends in the user object.
12+
- addFriend(user, "Pete") should return ["Sam", "Kira", "Tomo", "Pete"].
13+
*/
14+
let user = {
15+
name: 'Kenneth',
16+
age: 28,
17+
data: {
18+
username: 'kennethCodesAllDay',
19+
joinDate: 'March 26, 2016',
20+
organization: 'freeCodeCamp',
21+
friends: [
22+
'Sam',
23+
'Kira',
24+
'Tomo'
25+
],
26+
location: {
27+
city: 'San Francisco',
28+
state: 'CA',
29+
country: 'USA'
30+
}
31+
}
32+
};
33+
34+
function addFriend(userObj, friend) {
35+
// Only changed code below this line
36+
const friends = [...userObj.data.friends, friend];
37+
userObj.data.friends = friends;
38+
return friends;
39+
// Only changed code above this line
40+
}
41+
42+
console.log(addFriend(user, 'Pete'));

05-basic-data-structures/README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ freeCodeCamp module description:
88
99
### Exercises
1010

11-
- [X] [ 01 - Use an Array to Store a Collection of Data]()
12-
- [X] [ 02 - Access an Array's Contents Using Bracket Notation]()
13-
- [X] [ 03 - Add Items to an Array with push() and unshift()]()
14-
- [X] [ 04 - Remove Items from an Array with pop() and shift()]()
15-
- [X] [ 05 - Remove Items Using splice()]()
16-
- [X] [ 06 - Add Items Using splice()]()
17-
- [X] [ 07 - Copy Array Items Using slice()]()
18-
- [X] [ 08 - Copy an Array with the Spread Operator]()
19-
- [X] [ 09 - Combine Arrays with the Spread Operator]()
20-
- [X] [ 10 - Check For The Presence of an Element With indexOf()]()
21-
- [X] [ 11 - Iterate Through All an Array's Items Using For Loops]()
22-
- [X] [ 12 - Create complex multi-dimensional arrays]()
23-
- [X] [ 13 - Add Key-Value Pairs to JavaScript Objects]()
24-
- [X] [ 14 - Modify an Object Nested Within an Object]()
25-
- [X] [ 15 - Access Property Names with Bracket Notation]()
26-
- [X] [ 16 - Use the delete Keyword to Remove Object Properties]()
27-
- [X] [ 17 - Check if an Object has a Property]()
28-
- [X] [ 18 - Iterate Through the Keys of an Object with a for...in Statement]()
29-
- [ ] [ 19 - Generate an Array of All Object Keys with Object.keys()]()
30-
- [ ] [ 20 - Modify an Array Stored in an Object]()
11+
- [X] [ 01 - Use an Array to Store a Collection of Data](01-use-an-array-to-store-a-collection-of-data.js)
12+
- [X] [ 02 - Access an Array's Contents Using Bracket Notation](02-access-an-arrays-contents-using-bracket-notation.js)
13+
- [X] [ 03 - Add Items to an Array with push() and unshift()](03-add-items-to-an-array-with-push-and-unshift.js)
14+
- [X] [ 04 - Remove Items from an Array with pop() and shift()](04-remove-items-from-an-array-with-pop-and-shift.js)
15+
- [X] [ 05 - Remove Items Using splice()](05-remove-items-using-splice.js)
16+
- [X] [ 06 - Add Items Using splice()](06-add-items-using-splice.js)
17+
- [X] [ 07 - Copy Array Items Using slice()](07-copy-array-items-using-slice.js)
18+
- [X] [ 08 - Copy an Array with the Spread Operator](08-copy-an-array-with-the-spread-operator.js)
19+
- [X] [ 09 - Combine Arrays with the Spread Operator](09-combine-arrays-with-the-spread-operator.js)
20+
- [X] [ 10 - Check For The Presence of an Element With indexOf()](10-check-for-the-presence-of-an-element-with-index-of.js)
21+
- [X] [ 11 - Iterate Through All an Array's Items Using For Loops](11-iterate-through-all-an-arrays-items-using-for-loops.js)
22+
- [X] [ 12 - Create complex multi-dimensional arrays](12-create-complex-multi-dimensional-arrays.js)
23+
- [X] [ 13 - Add Key-Value Pairs to JavaScript Objects](13-add-key-value-pairs-to-js-objects.js)
24+
- [X] [ 14 - Modify an Object Nested Within an Object](14-modify-an-object-nested-within-an-object.js)
25+
- [X] [ 15 - Access Property Names with Bracket Notation](15-access-property-names-with-bracket-notation.js)
26+
- [X] [ 16 - Use the delete Keyword to Remove Object Properties](16-use-the-delete-keyword-to-remove-object-properties.js)
27+
- [X] [ 17 - Check if an Object has a Property](17-check-if-an-object-has-a-property.js)
28+
- [X] [ 18 - Iterate Through the Keys of an Object with a for...in Statement](18-iterate-through-the-keys-of-an-object-with-a-for-in-statement.js)
29+
- [X] [ 19 - Generate an Array of All Object Keys with Object.keys()](19-generate-an-array-of-all-object-keys-with-object-keys.js)
30+
- [X] [ 20 - Modify an Array Stored in an Object](20-modify-an-array-stored-in-an-object.js)
3131

3232
⬅️ [Back to main file](../README.md)
3333

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ freeCodeCamp course description:
1313
- [X] [ 02 - ES6](/02-es6/README.md)
1414
- [X] [ 03 - Regular Expressions](03-regular-expressions/README.md)
1515
- [X] [ 04 - Debugging](/04-debugging/README.md)
16-
- [ ] [ 05 - Basic Data Structures](/05-basic-data-structures/README.md)
16+
- [X] [ 05 - Basic Data Structures](/05-basic-data-structures/README.md)
1717
- [ ] [ 06 - Basic Algorithm Scripting]()
1818
- [ ] [ 07 - Object Oriented Programming]()
1919
- [ ] [ 08 - Functional Programming]()

0 commit comments

Comments
 (0)