Day 22B of Scrimba JavaScriptmas — Extract Matrix Column
This is the second challenge that pop up today in #scrimba #javascriptmas challenge. It totally different with 22A for me. It a lot easier. Or perhaps, i understand how to solve this because i finish the code before, i don’t know. The fact is i just need about 10–15 minutes to solve this. And the before i need almost 2–3 hours.
So this challenge is asking us, to give the value of column inside three array.
const matrix = [[1, 1, 1, 2], [0, 5, 0, 4], [2, 1, 3, 6]];const column = 2;// Result = [1, 0, 3]
After thinking about the algorithm it can be solved by using nested for loop statement. So the first thing i do, i build an empty array to contain the answer. Next, i build nested for loop, the first is two iterate the matrix, and the second is to iterate the index of the matrix. And after second iteration using push function, i save the value of matrix[i][2] into an empty array that i declare before. Don’t forget to use break, to stop, if iteration complete. Here is my solution code.
function extractMatrixColumn(matrix, column) {
// write code here.
const matrixColumn = []
for(i = 0; i<matrix.length;i++){
for(j=0;j<matrix[i].length;j++){
matrixColumn.push(matrix[i][2]);
break;
}
}
return matrixColumn
}
And here is the link for my answer
Hope u can do it better than me, Thanks!!!