|
1 | 1 | let str = 'In space, no one can hear you code.';
|
2 | 2 | let arr = ['B', 'n', 'n', 5];
|
3 | 3 |
|
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.'] |
5 | 8 |
|
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 (' '). |
7 | 10 |
|
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' |
9 | 15 |
|
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. |
11 | 25 | 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