Skip to content

Commit 57666e7

Browse files
committed
finished exercises
1 parent 2eb27ba commit 57666e7

File tree

6 files changed

+77
-15
lines changed

6 files changed

+77
-15
lines changed

arrays/exercises/part-five-arrays.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ let str = 'In space, no one can hear you code.';
22
let arr = ['B', 'n', 'n', 5];
33

44
//1) Use the split method on the string to identify the purpose of the parameter inside the ().
5-
5+
console.log(str.split());
6+
console.log(str.split('e'));
7+
console.log(str.split(' '));
8+
console.log(str.split(''));
69
//2) Use the join method on the array to identify the purpose of the parameter inside the ().
7-
10+
console.log(arr.join());
11+
console.log(arr.join('a'));
12+
console.log(arr.join(' '));
13+
console.log(arr.join(''));
814
//3) Do split or join change the original string/array?
9-
15+
console.log(str);
16+
console.log(arr);
1017
//4) We can take a comma-separated string and convert it into a modifiable array. Try it! Alphabetize the cargoHold string, and then combine the contents into a new string.
1118
let cargoHold = "water,space suits,food,plasma sword,batteries";
19+
20+
console.log(cargoHold.split(',').sort().join(','));
21+

arrays/exercises/part-four-arrays.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ let holdCabinet2 = ['orange drink', 'nerf toys', 'camera', 42, 'parsnip'];
44
//Explore the methods concat, slice, reverse, and sort to determine which ones alter the original array.
55

66
//1) Print the result of using concat on the two arrays. Does concat alter the original arrays? Verify this by printing holdCabinet1 after using the method.
7-
7+
console.log(holdCabinet1.concat(holdCabinet2));
8+
console.log(holdCabinet1);
89
//2) Print a slice of two elements from each array. Does slice alter the original arrays?
9-
10+
console.log(holdCabinet1.slice (2,4));
11+
console.log(holdCabinet2.slice (0,2));
12+
console.log(holdCabinet1);
1013
//3) reverse the first array, and sort the second. What is the difference between these two methods? Do the methods alter the original arrays?
14+
console.log(holdCabinet1.reverse());
15+
console.log(holdCabinet2.sort());
16+
console.log(holdCabinet1);
17+
console.log(holdCabinet2);

arrays/exercises/part-one-arrays.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//Create an array called practiceFile with the following entry: 273.15
2-
2+
let practiceFile = [273.15]
33
//Use the bracket notation method to add "42" and "hello" to the array. Add these new items one at a time. Print the array after each step to confirm the changes.
4-
4+
practiceFile[1] = ("42")
5+
console.log(practiceFile)
6+
practiceFile[2] = ("hello")
7+
console.log(practiceFile)
58
//Use a single .push() to add the following items: false, -4.6, and "87". Print the array to confirm the changes.
9+
practiceFile.push(false, -4.6, "87" )
10+
console.log(practiceFile)

arrays/exercises/part-six-arrays.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
//Arrays can hold different data types, even other arrays! A multi-dimensional array is one with entries that are themselves arrays.
22

33
//1) Define and initialize the arrays specified in the exercise to hold the name, chemical symbol and mass for different elements.
4-
4+
let element1 = ['hydrogen', 'H', 1.008];
5+
let element2 = ['helium', 'He', 4.003];
6+
let element26 = ['iron', 'Fe', 55.85];
57
//2) Define the array 'table', and use 'push' to add each of the element arrays to it. Print 'table' to see its structure.
6-
8+
let table = [];
9+
table.push(element1, element2, element26);
10+
console.log(table);
711
//3) Use bracket notation to examine the difference between printing 'table' with one index vs. two indices (table[][]).
8-
12+
console.log(table[1]);
13+
console.log(table[1][1]);
914
//4) Using bracket notation and the table array, print the mass of element1, the name for element 2 and the symbol for element26.
10-
15+
console.log(table[0][2]);
16+
console.log(table[1][0]);
17+
console.log(table[2][1]);
1118
//5) 'table' is an example of a 2-dimensional array. The first “level” contains the element arrays, and the second level holds the name/symbol/mass values. Experiment! Create a 3-dimensional array and print out one entry from each level in the array.
19+
let arr1 = [1,2,3];
20+
let arr2 = [4,5,6];
21+
let arr3 = [7,8,9];
22+
let arr4 = [10,11,12];
23+
let arr5 = [13,14,15];
24+
let arr6 = [16,17,18];
25+
let arr7 = [19,20,21];
26+
let arr8 = [22,23,24];
27+
let arr9 = [25,26,27];
28+
29+
let item1 = [arr1, arr2, arr3];
30+
let item2 = [arr4, arr5, arr6];
31+
let item3 = [arr7, arr8, arr9];
32+
33+
let array3D = [];
34+
array3D.push(item1,item2,item3);
35+
console.log (array3D);
36+
console.log(array3D[0]);
37+
console.log(array3D[0][0]);
38+
console.log(array3D[0][0][0]);

arrays/exercises/part-three-arrays.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ let cargoHold = [1138, 'space suits', 'parrot', 'instruction manual', 'meal pack
33
//Use splice to make the following changes to the cargoHold array. Be sure to print the array after each step to confirm your updates.
44

55
//1) Insert the string 'keys' at index 3 without replacing any other entries.
6-
6+
cargoHold.splice(3,0,"keys")
7+
console.log(cargoHold)
78
//2) Remove ‘instruction manual’ from the array. (Hint: indexOf is helpful to avoid manually counting an index).
8-
9+
cargoHold.splice(4,1)
10+
console.log(cargoHold)
911
//3) Replace the elements at indexes 2 - 4 with the items ‘cat’, ‘fob’, and ‘string cheese’.
12+
cargoHold.splice(2,3, 'cat', 'fob', 'string cheese')
13+
console.log(cargoHold)

arrays/exercises/part-two-arrays.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];
22

33
//1) Use bracket notation to replace ‘slinky’ with ‘space tether’. Print the array to confirm the change.
4-
4+
cargoHold[5] = ('space tether');
5+
6+
console.log (cargoHold)
57
//2) Remove the last item from the array with pop. Print the element removed and the updated array.
8+
console.log(cargoHold.pop());
69

10+
console.log(cargoHold);
711
//3) Remove the first item from the array with shift. Print the element removed and the updated array.
12+
console.log(cargoHold.shift());
813

14+
console.log(cargoHold);
915
//4) Unlike pop and shift, push and unshift require arguments inside the (). Add the items 1138 and ‘20 meters’ to the the array - the number at the start and the string at the end. Print the updated array to confirm the changes.
10-
16+
cargoHold.push('20 meters');
17+
cargoHold.unshift(1138);
18+
console.log(cargoHold);
1119
//5) Use a template literal to print the final array and its length.
20+
console.log(`The array ${cargoHold} has a length of ${cargoHold.length}.`);

0 commit comments

Comments
 (0)