// 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]);
});
});