str in mas

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
var str = 'I love bananas and schocolate ';
var mas = [];
var a = '';
for (var i = 0, n = 0; i < str.length; i++) {
if (str[i] != ' ') {
a += str[i];
if (i == str.length - 1) {
mas[n] = a;
}
} else if (str[i + 1] != ' ') {
mas[n] = a;
n++;
a = '';
}
}
console.log(mas);