Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeanjmichel/5c3723552e0a8e2123226b97b6ea494c to your computer and use it in GitHub Desktop.
Save jeanjmichel/5c3723552e0a8e2123226b97b6ea494c to your computer and use it in GitHub Desktop.
Manipulating two-dimensional arrays in JavaScript
var matrix = {
create: function(rows, columns) {
var newMatrix = [];
for (var i = 0; i < rows; i++) {
var row = [];
for (var j = 0; j < columns; j++) {
row.push(0); // Inicializa a matriz com valores zero
}
newMatrix.push(row);
}
return newMatrix;
},
set: function(matrix, row, column, value) {
if (row >= 0 && row < matrix.length && column >= 0 && column < matrix[0].length) {
matrix[row][column] = value;
} else {
console.error("Índices fora do alcance da matriz.");
}
},
get: function(matrix, row, column) {
if (row >= 0 && row < matrix.length && column >= 0 && column < matrix[0].length) {
return matrix[row][column];
} else {
console.error("Índices fora do alcance da matriz.");
return undefined;
}
}
};
var m4x4 = matrix.create(4, 4);
matrix.set(m4x4, 0, 0, 1); matrix.set(m4x4, 0, 1, 0); matrix.set(m4x4, 0, 2, 0); matrix.set(m4x4, 0, 3, 0);
matrix.set(m4x4, 1, 0, 0); matrix.set(m4x4, 1, 1, 1); matrix.set(m4x4, 1, 2, 0); matrix.set(m4x4, 1, 3, 0);
matrix.set(m4x4, 2, 0, 0); matrix.set(m4x4, 2, 1, 0); matrix.set(m4x4, 2, 2, 1); matrix.set(m4x4, 2, 3, 0);
matrix.set(m4x4, 3, 0, 0); matrix.set(m4x4, 3, 1, 0); matrix.set(m4x4, 3, 2, 0); matrix.set(m4x4, 3, 3, 1);
//console.log(m4x4);
printMatrix(m4x4);
function printMatrix(matrix) {
for (var i = 0; i < matrix.length; i++) {
console.log(matrix[i]);
}
}
@jeanjmichel
Copy link
Author

jeanjmichel commented May 17, 2024

Two-dimensional arrays are a common data structure in many programming languages, but JavaScript does not have a built-in way to create and manipulate them. However, there are some workarounds and tricks that can help us achieve this functionality. Here I will share how I used an AI tool (Chat GPT) to implement a solution manipulate two-dimensional arrays in JavaScript after I read a blog article that sparked my interest.

I saw a blog post about how to make arrays work like matrices in JavaScript, and I remembered the programming logic exercises we did in school with nested loops.

I read the author's suggestions, which went from simple nested loops to Array.from and the Spread Operator, but as a C# fan, I wanted a more reusable way to do this, how could I make something that takes parameters for making the matrix and then coordinates for changing the cells in it?

So, I asked Chat GPT to help me make a function in JavaScript that had 3 methods:

  1. A constructor that made a matrix with the size parameters I gave in rows and columns.

  2. A method that set a value to a coordinate of the matrix that I would give as a parameter with the value.

  3. A method that got the value of a matrix coordinate given that I would give that coordinate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment