Skip to content

Commit 0981519

Browse files
committed
array exercises completed
1 parent cc0eb56 commit 0981519

File tree

7 files changed

+73
-12
lines changed

7 files changed

+73
-12
lines changed

arrays/exercises/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

arrays/exercises/part-five-arrays.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
let str = 'In space, no one can hear you code.';
22
let arr = ['B', 'n', 'n', 5];
33

4-
//1) Use the split method on the string to identify the purpose of the parameter inside the ().
54

5+
//1) Use the split method on the string to identify the purpose of the parameter inside the ().
6+
console.log(str.split());
7+
console.log(str.split('e'));
8+
console.log(str.split(' '));
9+
console.log(str.split(''));
610
//2) Use the join method on the array to identify the purpose of the parameter inside the ().
7-
11+
console.log(arr.join());
12+
console.log(arr.join('a'));
13+
console.log(arr.join(' '));
14+
console.log(arr.join(''));
815
//3) Do split or join change the original string/array?
9-
16+
//no
17+
console.log(str);
18+
console.log(arr);
1019
//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.
11-
let cargoHold = "water,space suits,food,plasma sword,batteries";
20+
let cargoHold = "water,space suits,food,plasma,sword,batteries";
21+
console.log(cargoHold.split(',').sort().join(','));

arrays/exercises/part-four-arrays.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ 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+
holdCabinet1.concat();
8+
holdCabinet2.concat();
9+
console.log(holdCabinet1);
10+
console.log(holdCabinet2);
11+
console.log(holdCabinet1.concat(holdCabinet2));
712

813
//2) Print a slice of two elements from each array. Does slice alter the original arrays?
14+
console.log(holdCabinet1.slice(0, 2));
15+
holdCabinet2.slice(2);
16+
console.log(holdCabinet1);
17+
console.log(holdCabinet2);
918

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+
holdCabinet2.sort();
22+
console.log(holdCabinet1);
23+
console.log(holdCabinet2);

arrays/exercises/part-one-arrays.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
//Create an array called practiceFile with the following entry: 273.15
2-
2+
let practiceFile = [273.15];
3+
//console.log(practiceFile);
34
//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-
5+
practiceFile.push(42)
6+
console.log(practiceFile);
7+
practiceFile.push("hello");
8+
console.log(practiceFile)
59
//Use a single .push() to add the following items: false, -4.6, and "87". Print the array to confirm the changes.
10+
practiceFile.push(false, -4.6, "87");
11+
console.log(practiceFile);

arrays/exercises/part-six-arrays.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
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+
let element1 = ['hydrogen', 'H', 1.008];
5+
let element2 = ['helium', 'He', 4.003];
6+
let element26 = ['iron', 'Fe', 55.85];
7+
console.log(element1);
8+
console.log(element2);
9+
console.log(element26);
410

511
//2) Define the array 'table', and use 'push' to add each of the element arrays to it. Print 'table' to see its structure.
12+
let table = []
13+
table.push(element1, element2, element26);
14+
console.log(table);
615

716
//3) Use bracket notation to examine the difference between printing 'table' with one index vs. two indices (table[][]).
17+
console.log(table[1]);
18+
console.log(table[1][2])
819

920
//4) Using bracket notation and the table array, print the mass of element1, the name for element 2 and the symbol for element26.
10-
21+
console.log(table[0][2])
22+
console.log(table[1][0])
23+
console.log(table[2][1])
1124
//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.
25+
26+

arrays/exercises/part-three-arrays.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ 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).
9+
cargoHold.splice(4, 1);
10+
console.log(cargoHold);
811

912
//3) Replace the elements at indexes 2 - 4 with the items ‘cat’, ‘fob’, and ‘string cheese’.
13+
cargoHold.splice(2, 3, 'cat', 'fob', 'string cheese');
14+
console.log(cargoHold);

arrays/exercises/part-two-arrays.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
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.
6-
7+
cargoHold.pop()
8+
console.log(cargoHold);
79
//3) Remove the first item from the array with shift. Print the element removed and the updated array.
8-
10+
cargoHold.shift();
11+
console.log(cargoHold);
912
//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-
13+
cargoHold.unshift(1138);
14+
cargoHold.push("20 meters");
15+
console.log(cargoHold);
1116
//5) Use a template literal to print the final array and its length.
17+
console.log(`The array ${cargoHold} has a length of ${cargoHold.length}.`);

0 commit comments

Comments
 (0)