Skip to content

Commit fb70a48

Browse files
committed
Arrays Done
1 parent cde6b5c commit fb70a48

6 files changed

+98
-10
lines changed

arrays/exercises/part-five-arrays.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@ 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+
console.log(str.split());
6+
console.log(str.split('e'));
7+
console.log(str.split(' '));
8+
console.log(str.split(''));
9+
// the purpose is to select with element to be affected
510

611
//2) Use the join method on the array to identify the purpose of the parameter inside the ().
12+
console.log(arr.join());
13+
console.log(arr.join('a'));
14+
console.log(arr.join(' '));
15+
console.log(arr.join(''));
16+
17+
// the purpose is to select with element to be affected
718

819
//3) Do split or join change the original string/array?
20+
console.log(str);
21+
console.log(arr);
22+
//they do not
923

1024
//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.
1125
let cargoHold = "water,space suits,food,plasma sword,batteries";
26+
console.log(cargoHold.split(',').sort().join(','));

arrays/exercises/part-four-arrays.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,22 @@ 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+
let tools = holdCabinet1.concat(holdCabinet2);
8+
console.log(tools);
9+
// it does not, it stores the new values under 'tools'
10+
console.log(holdCabinet1);
811
//2) Print a slice of two elements from each array. Does slice alter the original arrays?
9-
12+
let misc = holdCabinet1.slice(0,2);
13+
let misc2 = holdCabinet2.slice(3,5);
14+
console.log(misc);
15+
console.log(holdCabinet1);
16+
console.log(misc2);
17+
console.log(holdCabinet2);
18+
// again, it does not
1019
//3) reverse the first array, and sort the second. What is the difference between these two methods? Do the methods alter the original arrays?
20+
holdCabinet1.reverse();
21+
console.log(holdCabinet1);
22+
holdCabinet2.sort();
23+
console.log(holdCabinet2);
24+
// sort() sorts the elements alphabetically and in ascending order, while reverse() simply puts them in opposite order
25+
// They both overwrite the original array

arrays/exercises/part-one-arrays.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
//Create an array that can hold 4 items name practiceFile.
2-
2+
let practiceFile = [
3+
273.15
4+
]
35
//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-
6+
practiceFile.push(42);
7+
console.log(practiceFile);
8+
practiceFile.push('hello')
9+
console.log(practiceFile);
510
//Use a SetValue to add the items "false", and "-4.6" to the array. Print the array to confirm the changes.
11+
practiceFile.push(false, -4.6, '87');
12+
console.log(practiceFile)

arrays/exercises/part-six-arrays.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
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+
element1 = ['hydrogen', 'H', 1.008]
5+
element2 = ['helium', 'He', 4.003]
6+
element26 = ['iron', 'Fe', 55.85]
47

5-
//2) Define the array 'table', and use 'push' to add each of the element arrays to it. Print 'table' to see its structure.
68

9+
//2) Define the array 'table', and use 'push' to add each of the element arrays to it. Print 'table' to see its structure.
10+
table = []
11+
table.push(element1, element2, element26);
12+
console.log(table);
713
//3) Use bracket notation to examine the difference between printing 'table' with one index vs. two indices (table[][]).
8-
14+
console.log(table[0]);
15+
console.log(table[0][1]);
916
//4) Using bracket notation and the table array, print the mass of element1, the name for element 2 and the symbol for element26.
10-
17+
console.log(table[0][2]);
18+
console.log(table[1][0]);
19+
console.log(table[2][1]);
1120
//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.
21+
22+
let dimension3 = [
23+
[
24+
[
25+
"Level 1, Entry 1",
26+
"Level 1, Entry 2"
27+
],
28+
[
29+
"Level 1, Entry 3",
30+
"Level 1, Entry 4"
31+
]
32+
],
33+
[
34+
[
35+
"Level 2, Entry 1",
36+
"Level 2, Entry 2"
37+
],
38+
[
39+
"Level 2, Entry 3",
40+
"Level 2, Entry 4"
41+
]
42+
]
43+
];
44+
45+
console.log(dimension3[0][0][0]);
46+
console.log(dimension3[1][1][0]);

arrays/exercises/part-three-arrays.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ 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+
console.log(cargoHold.indexOf('instruction manual'));
10+
cargoHold.splice(4, 1);
11+
console.log(cargoHold);
912
//3) Replace the elements at indexes 2 - 4 with the items ‘cat’, ‘fob’, and ‘string cheese’.
13+
14+
cargoHold.splice(2,3, 'cat', 'fob', 'string cheese');
15+
console.log(cargoHold);

arrays/exercises/part-two-arrays.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
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+
console.log(cargoHold)
56
//2) Remove the last item from the array with pop. Print the element removed and the updated array.
7+
console.log(cargoHold.pop());
8+
console.log(cargoHold);
69

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

914
//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.
15+
cargoHold.unshift(1138);
16+
console.log(cargoHold);
17+
cargoHold.push('20 meters');
18+
console.log(cargoHold);
1019

1120
//5) Use a template literal to print the final array and its length.
21+
console.log(`the final list is ${cargoHold} and it is ${cargoHold.length} long`);

0 commit comments

Comments
 (0)