Skip to content

Commit 1ce34f2

Browse files
committed
Finish data structures adn basic algorithm challenges
1 parent 5284701 commit 1ce34f2

5 files changed

+198
-0
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Basic Algorithm Scripting: Slice and Splice
2+
// You are given two arrays and an index.
3+
4+
// Use the array methods slice and splice to copy each element of the first array into the second array, in order.
5+
6+
// Begin inserting elements at index n of the second array.
7+
8+
// Return the resulting array. The input arrays should remain the same after the function runs.
9+
10+
// Remember to use Read-Search-Ask if you get stuck. Write your own code.
11+
12+
// frankenSplice([1, 2, 3], [4, 5], 1) should return [4, 1, 2, 3, 5].
13+
// frankenSplice([1, 2], ["a", "b"], 1) should return ["a", 1, 2, "b"].
14+
// frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2) should return ["head", "shoulders", "claw", "tentacle", "knees", "toes"].
15+
// All elements from the first array should be added to the second array in their original order.
16+
// The first array should remain the same after the function runs.
17+
// The second array should remain the same after the function runs.
18+
19+
function frankenSplice(arr1, arr2, n) {
20+
// It's alive. It's alive!
21+
let firstHalf = arr2.slice(0, n);
22+
let secondHalf = arr2.slice(n, arr2.length);
23+
24+
return [...firstHalf, ...arr1, ...secondHalf];
25+
}
26+
27+
frankenSplice([1, 2, 3], [4, 5, 6], 1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Basic Data Structures: Check if an Object has a Property
2+
// Now we can add, modify, and remove keys from objects. But what if we just wanted to know if an object has a specific property? JavaScript provides us with two different ways to do this. One uses the hasOwnProperty() method and the other uses the in keyword. If we have an object users with a property of Alan, we could check for its presence in either of the following ways:
3+
4+
// users.hasOwnProperty('Alan');
5+
// 'Alan' in users;
6+
// // both return true
7+
8+
// We've created an object, users, with some users in it and a function isEveryoneHere, which we pass the users object to as an argument. Finish writing this function so that it returns true only if the users object contains all four names, Alan, Jeff, Sarah, and Ryan, as keys, and false otherwise.
9+
10+
// The users object only contains the keys Alan, Jeff, Sarah, and Ryan
11+
// The function isEveryoneHere returns true if Alan, Jeff, Sarah, and Ryan are properties on the users object
12+
// The function isEveryoneHere returns false if Alan, Jeff, Sarah, and Ryan are not properties on the users object
13+
14+
let users = {
15+
Alan: {
16+
age: 27,
17+
online: true
18+
},
19+
Jeff: {
20+
age: 32,
21+
online: true
22+
},
23+
Sarah: {
24+
age: 48,
25+
online: true
26+
},
27+
Ryan: {
28+
age: 19,
29+
online: true
30+
}
31+
};
32+
33+
function isEveryoneHere(obj) {
34+
// change code below this line
35+
return obj.hasOwnProperty('Alan')
36+
&& obj.hasOwnProperty('Jeff')
37+
&& obj.hasOwnProperty('Sarah')
38+
&& obj.hasOwnProperty('Ryan');
39+
// change code above this line
40+
}
41+
42+
console.log(isEveryoneHere(users));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Basic Data Structures: Generate an Array of All Object Keys with Object.keys()
2+
// We can also generate an array which contains all the keys stored in an object using the Object.keys() method and passing in an object as the argument. This will return an array with strings representing each property in the object. Again, there will be no specific order to the entries in the array.
3+
4+
5+
// Finish writing the getArrayOfUsers function so that it returns an array containing all the properties in the object it receives as an argument.
6+
7+
// The users object only contains the keys Alan, Jeff, Sarah, and Ryan
8+
// The getArrayOfUsers function returns an array which contains all the keys in the users object
9+
10+
let users = {
11+
Alan: {
12+
age: 27,
13+
online: false
14+
},
15+
Jeff: {
16+
age: 32,
17+
online: true
18+
},
19+
Sarah: {
20+
age: 48,
21+
online: false
22+
},
23+
Ryan: {
24+
age: 19,
25+
online: true
26+
}
27+
};
28+
29+
function getArrayOfUsers(obj) {
30+
// change code below this line
31+
return Object.keys(obj);
32+
// change code above this line
33+
}
34+
35+
console.log(getArrayOfUsers(users));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Basic Data Structures: Iterate Through the Keys of an Object with a for...in Statement
2+
// Sometimes you may need to iterate through all the keys within an object. This requires a specific syntax in JavaScript called a for...in statement. For our users object, this could look like:
3+
4+
// for (let user in users) {
5+
// console.log(user);
6+
// };
7+
8+
// // logs:
9+
// Alan
10+
// Jeff
11+
// Sarah
12+
// Ryan
13+
// In this statement, we defined a variable user, and as you can see, this variable was reset during each iteration to each of the object's keys as the statement looped through the object, resulting in each user's name being printed to the console.
14+
15+
// NOTE:
16+
// Objects do not maintain an ordering to stored keys like arrays do; thus a keys position on an object, or the relative order in which it appears, is irrelevant when referencing or accessing that key.
17+
18+
19+
// We've defined a function, countOnline; use a for...in statement within this function to loop through the users in the users object and return the number of users whose online property is set to true.
20+
21+
// The users object contains users Jeff and Ryan with online set to true and users Alan and Sarah with online set to false
22+
// The function countOnline returns the number of users with the online property set to true
23+
24+
let users = {
25+
Alan: {
26+
age: 27,
27+
online: false
28+
},
29+
Jeff: {
30+
age: 32,
31+
online: true
32+
},
33+
Sarah: {
34+
age: 48,
35+
online: false
36+
},
37+
Ryan: {
38+
age: 19,
39+
online: true
40+
}
41+
};
42+
43+
function countOnline(obj) {
44+
// change code below this line
45+
let count = 0;
46+
for (let user in obj) {
47+
if(obj[user].online == true) {
48+
count++;
49+
}
50+
}
51+
52+
return count;
53+
// change code above this line
54+
}
55+
56+
console.log(countOnline(users));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Basic Data Structures: Modify an Array Stored in an Object
2+
// Now you've seen all the basic operations for JavaScript objects. You can add, modify, and remove key-value pairs, check if keys exist, and iterate over all the keys in an object. As you continue learning JavaScript you will see even more versatile applications of objects. Additionally, the optional Advanced Data Structures lessons later in the curriculum also cover the ES6 Map and Set objects, both of which are similar to ordinary objects but provide some additional features. Now that you've learned the basics of arrays and objects, you're fully prepared to begin tackling more complex problems using JavaScript!
3+
4+
5+
// Take a look at the object we've provided in the code editor. The 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.
6+
7+
// The user object has name, age, and data keys
8+
// 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
9+
// addFriend(user, "Pete") should return ["Sam", "Kira", "Tomo", "Pete"]
10+
11+
let user = {
12+
name: 'Kenneth',
13+
age: 28,
14+
data: {
15+
username: 'kennethCodesAllDay',
16+
joinDate: 'March 26, 2016',
17+
organization: 'freeCodeCamp',
18+
friends: [
19+
'Sam',
20+
'Kira',
21+
'Tomo'
22+
],
23+
location: {
24+
city: 'San Francisco',
25+
state: 'CA',
26+
country: 'USA'
27+
}
28+
}
29+
};
30+
31+
function addFriend(userObj, friend) {
32+
// change code below this line
33+
userObj.data.friends.push(friend);
34+
return userObj.data.friends;
35+
// change code above this line
36+
}
37+
38+
console.log(addFriend(user, 'Pete'));

0 commit comments

Comments
 (0)