Skip to content

Commit 4e70d2e

Browse files
authored
Completed part 5
1 parent e65941b commit 4e70d2e

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

arrays/exercises/part-five-arrays.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
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 ().
4+
// 1) Use the split method on the string to identify the purpose of the parameter inside the ().
5+
let splitStr = str.split(' ');
6+
console.log(splitStr);
7+
// ['In', 'space,', 'no', 'one', 'can', 'hear', 'you', 'code.']
58

6-
//2) Use the join method on the array to identify the purpose of the parameter inside the ().
9+
// The parameter inside the split method specifies the delimiter at which the string should be split. In this case, it's a space character (' ').
710

8-
//3) Do split or join change the original string/array?
11+
// 2) Use the join method on the array to identify the purpose of the parameter inside the ().
12+
let joinedArr = arr.join('-');
13+
console.log(joinedArr);
14+
// 'B-n-n-5'
915

10-
//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.
16+
// The parameter inside the join method specifies the separator that will be placed between each element of the array in the resulting string. In this case, it's a hyphen ('-').
17+
18+
// 3) Do split or join change the original string/array?
19+
console.log(str);
20+
// 'In space, no one can hear you code.' - split does not change the original string.
21+
console.log(arr);
22+
// ['B', 'n', 'n', 5] - join does not change the original array.
23+
24+
// 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+
let cargoArray = cargoHold.split(',');
27+
cargoArray.sort();
28+
let newCargoHold = cargoArray.join(',');
29+
console.log(newCargoHold);
30+
// 'batteries,food,plasma sword,space suits,water'

0 commit comments

Comments
 (0)