Skip to content

Commit dae935a

Browse files
committed
solution
1 parent a678c0e commit dae935a

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

lab/exercises/10-mixed/even-first.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
function evenFirst(array) {
1+
function sortArrayByParity(A) {
22
let lo = 0;
3-
let hi = 1;
4-
while (hi < array.length) {
5-
if (array[hi] % 2 === 0) {
6-
[array[hi], array[lo]] = [array[lo], array[hi]];
3+
let hi = A.length - 1;
4+
while (lo < hi) {
5+
if (A[lo] % 2 === 0) {
76
lo++;
7+
} else {
8+
[A[hi], A[lo]] = [A[lo], A[hi]];
9+
hi--;
810
}
9-
hi++;
1011
}
11-
return array;
12+
return A;
1213
}
1314

14-
module.exports = evenFirst;
15+
module.exports = sortArrayByParity;
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
const fn = require('./even-first');
22

33
describe('Sort even first and then odd', () => {
4-
it('should work', () => {
4+
it('should work when start with even', () => {
5+
const actual = fn([0, 1, 2]);
6+
expect(actual.slice(0, 2)).toEqual(expect.arrayContaining([0, 2]));
7+
expect(actual.slice(2)).toEqual(expect.arrayContaining([1]));
8+
});
9+
10+
it('should work when start with odd', () => {
511
const actual = fn([5, 4, 3, 7, 8, 3, 2]);
612
expect(actual.slice(0, 3)).toEqual(expect.arrayContaining([4, 8, 2]));
713
expect(actual.slice(3)).toEqual(expect.arrayContaining([5, 3, 7, 3]));
814
});
15+
16+
it('should work with empty', () => {
17+
const actual = fn([]);
18+
expect(actual).toEqual([]);
19+
});
920
});

0 commit comments

Comments
 (0)