Answer the question
In order to leave comments, you need to log in
How to display matrix in DOM?
I need to create a 5x5 matrix and output it.
I know how to create it:
function matrixArray(rows,columns){
var arr = [];
for(var i=0; i<rows; i++){
arr[i] = [];
for(var j=0; j<columns; j++){
arr[i][j] = Math.floor(Math.random() * 20) + 10;
}
}
return arr;
}
var myMatrix = matrixArray(5,5);
Answer the question
In order to leave comments, you need to log in
function createMatrix(rows, cols, min, max) {
const matrix = [];
for (let i = 0; i < rows; i++) {
matrix[i] = [];
for (let j = 0; j < cols; j++) {
matrix[i][j] = Math.floor(Math.random() * (max - min + 1)) + min;
}
}
return matrix;
}
document.querySelector('body').insertAdjacentHTML('beforeend', `
<table>${createMatrix(5, 5, 10, 30).map(n => `
<tr>${n.map(m => `
<td>${m}</td>`).join('')}
</tr>`).join('')}
</table>
`);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question