1
5
6var sortArrayByParityII = function(A) {
7 let even = A.filter(value => value % 2 === 0);
8 let odd = A.filter(value => value % 2 === 1);
9
10 for (let i = 0, l = A.length; i < l; i++) {
11 if(i%2 === 0) {
12 A[i] = even.shift();
13 } else {
14 A[i] = odd.shift();
15 }
16 }
17
18 return A;
19};
20
21
22var sortArrayByParityII = function(A) {
23 let result = Array(A.length);
24 let odd = 1, even = 0;
25 A.forEach(el => {
26 if (el % 2 === 0) {
27 result[even] = el;
28 even += 2
29 } else {
30 result[odd] = el;
31 odd += 2
32 }
33 });
34
35 return result
36};
37
38ensureEqual(sortArrayByParityII([4,2,5,7]), [4,5,2,7], 'test 1');