scrimba
Solution for Day 22 of #javascriptmas
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Solution for Day 22 of #javascriptmas
AboutCommentsNotes
Solution for Day 22 of #javascriptmas
Expand for more info
main.js
run
preview
console
// Solution 1
function extractMatrixColumn(matrix, column) {
let result = []
matrix.forEach(inner => {
inner.forEach((colValue, index) => {
if(index === column) {
result.push(colValue)
}
})
})
return result
}

// Solution 2
// function extractMatrixColumn(matrix, column) {
// let result = []
// matrix.map( x => {
// if(x[column] !== undefined) {
// result.push(x[column])
// }
// })
// return result
// }


/**
* Test Suite
*/
describe('extractMatrixColumn()', () => {
it('returns largest positive integer possible for digit count', () => {
const matrix = [[1, 1, 1, 2], [0, 5, 0, 4], [2, 1, 3, 6]];
const column = 2;
const result = extractMatrixColumn(matrix, column);
console.log("result: ", result);
expect(result).toEqual([1, 0, 3]);
});

it('returns empty array in case of index out of limit', () => {
// arrange
const matrix = [[1, 1, 1, 2], [0, 5, 0, 4], [2, 1, 3, 6]];
const column = 4;
const result = extractMatrixColumn(matrix, column);
console.log("result1: ", result);
expect(result).toEqual([]);
});

it('returns empty array in case of negative index', () => {
const matrix = [[1, 1, 1, 2], [0, 5, 0, 4], [2, 1, 3, 6]];
const column = -4;
const result = extractMatrixColumn(matrix, column);
console.log("result2: ", result);
expect(result).toEqual([]);
});

it('returns largest positive integer possible in case of mixed length inner arrays', () => {
const matrix = [[1, 1, 1, 2], [0, 5, 0, 4], [2, 1, 3]];
const column = 3;
const result = extractMatrixColumn(matrix, column);
console.log("result3: ", result);
expect(result).toEqual([2,4]);
});
});
Console
"result3: "
,
[
2
,
4
]
,
"result2: "
,
[]
,
"result1: "
,
[]
,
"result: "
,
[
1
,
0
,
3
]
,
/index.html
LIVE