Description
- Write a function to split a string and convert it into an array of words.
Solution 01
function stringToArray(str) {
const arr = str.split(' ');
let result = [];
for (let i = 0; i < arr.length; i++) {
result.push(arr[i]);
}
return result;
}
stringToArray('Hello World'); // ['Hello', 'World']
stringToArray('How are you?'); // ['How', 'are', 'you?']
Solution 02
function stringToArray(str) {
return str.split(' ');
}
stringToArray('Hello World'); // ['Hello', 'World']
stringToArray('How are you?'); // ['How', 'are', 'you?']