|
1 | 1 | let holdCabinet1 = ['duct tape', 'gum', 3.14, false, 6.022e23];
|
2 | 2 | let holdCabinet2 = ['orange drink', 'nerf toys', 'camera', 42, 'parsnip'];
|
3 | 3 |
|
4 |
| -//Explore the methods concat, slice, reverse, and sort to determine which ones alter the original array. |
| 4 | +// 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. |
| 5 | +let combinedCabinet = holdCabinet1.concat(holdCabinet2); |
| 6 | +console.log(combinedCabinet); |
| 7 | +// ['duct tape', 'gum', 3.14, false, 6.022e23, 'orange drink', 'nerf toys', 'camera', 42, 'parsnip'] |
| 8 | +console.log(holdCabinet1); |
| 9 | +// ['duct tape', 'gum', 3.14, false, 6.022e23] - concat does not alter the original array. |
5 | 10 |
|
6 |
| -//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. |
| 11 | +// 2) Print a slice of two elements from each array. Does slice alter the original arrays? |
| 12 | +let sliceCabinet1 = holdCabinet1.slice(1, 3); |
| 13 | +let sliceCabinet2 = holdCabinet2.slice(2, 4); |
| 14 | +console.log(sliceCabinet1); |
| 15 | +// ['gum', 3.14] |
| 16 | +console.log(sliceCabinet2); |
| 17 | +// ['camera', 42] |
| 18 | +console.log(holdCabinet1); |
| 19 | +// ['duct tape', 'gum', 3.14, false, 6.022e23] - slice does not alter the original array. |
| 20 | +console.log(holdCabinet2); |
| 21 | +// ['orange drink', 'nerf toys', 'camera', 42, 'parsnip'] - slice does not alter the original array. |
7 | 22 |
|
8 |
| -//2) Print a slice of two elements from each array. Does slice alter the original arrays? |
| 23 | +// 3) Reverse the first array, and sort the second. What is the difference between these two methods? Do the methods alter the original arrays? |
| 24 | +holdCabinet1.reverse(); |
| 25 | +console.log(holdCabinet1); |
| 26 | +// [6.022e23, false, 3.14, 'gum', 'duct tape'] - reverse alters the original array. |
9 | 27 |
|
10 |
| -//3) reverse the first array, and sort the second. What is the difference between these two methods? Do the methods alter the original arrays? |
| 28 | +holdCabinet2.sort(); |
| 29 | +console.log(holdCabinet2); |
| 30 | +// [42, 'camera', 'nerf toys', 'orange drink', 'parsnip'] - sort alters the original array. |
0 commit comments