BEGIN write your solution here function solution str let result let wo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// BEGIN (write your solution here)
function solution(str) {
let result = [];
let word = '';
let state = 'before';
for(let i = 0; i < str.length; i += 1) {
const symbol = str[i];
switch(state) {
case 'before':
if (symbol !== ' ' && symbol !== '\n') {
state = 'in';
word += symbol;
}
break;
case 'in':
if (symbol === ' ') {
state = 'after';
result.push(word);
word = '';
} else if (symbol === '\n') {
state = 'before';
result.push(word);
word = '';
}
else {
word += symbol;
if (i === str.length - 1) {
result.push(word);
state = 'after';
}
}
break;
case 'after':
if (symbol === '\n') {
state = 'before';
}
break;
}
}
return result;
}
export default solution;
// END