Last active
January 19, 2016 15:49
-
-
Save dheavy/e15c0a27a6c61653970d to your computer and use it in GitHub Desktop.
JS array matrix implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES5 | |
Array.matrix = function(numRows, numCols, initialValue) { | |
var matrix = []; | |
for (var i = 0; i < numRows; ++i) { | |
var columns = []; | |
for (var j = 0; j < numCols; ++j) { | |
columns[j] = initialValue; | |
} | |
matrix[i] = columns; | |
} | |
return matrix; | |
}; | |
// ES6 | |
Array.matrix = (numRows, numCols, initialValue) => { | |
let matrix = []; | |
for (let i = 0; i < numRows; ++i) { | |
let columns = []; | |
for (let j = 0; j < numCols; ++j) { | |
columns[j] = initialValue; | |
} | |
matrix[i] = columns; | |
} | |
return matrix; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment