Skip to content

Commit 47bc42a

Browse files
committed
Adjust transpose function not to eat '0' s
1 parent 277ff9a commit 47bc42a

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/lib/__tests__/transpose-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('transpose', () => {
1818
});
1919

2020
it('correctly transposes 2d arrays', () => {
21-
const originalArray = [[1, 2, 3], [9, 8, 7]];
21+
const originalArray = [[1, 2, 3], [9, 8, 0]];
2222
const transposedArray = transpose(originalArray);
2323

2424
expect(transposedArray.length).toBe(3);
@@ -32,7 +32,7 @@ describe('transpose', () => {
3232
expect(transposedArray[1][0]).toBe(2);
3333
expect(transposedArray[1][1]).toBe(8);
3434
expect(transposedArray[2][0]).toBe(3);
35-
expect(transposedArray[2][1]).toBe(7);
35+
expect(transposedArray[2][1]).toBe(0);
3636
});
3737

3838
it('correctly fills non symmetrical 2d arrays', () => {

src/lib/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ function transpose(originalArray) {
9595
newArray[innerIndex] = [];
9696
}
9797

98-
const value = originalArray[outerIndex][innerIndex]
99-
? originalArray[outerIndex][innerIndex]
100-
: null;
98+
const value =
99+
typeof originalArray[outerIndex][innerIndex] !== 'undefined'
100+
? originalArray[outerIndex][innerIndex]
101+
: null;
101102
newArray[innerIndex].push(value);
102103
}
103104
}

0 commit comments

Comments
 (0)