Skip to content

Instantly share code, notes, and snippets.

@bartwttewaall
Created March 20, 2024 13:04
Show Gist options
  • Save bartwttewaall/a0a26285d040431a6c0a86e772531e59 to your computer and use it in GitHub Desktop.
Save bartwttewaall/a0a26285d040431a6c0a86e772531e59 to your computer and use it in GitHub Desktop.
Traverse a 2-dimensional array in a single for-loop
function traverse (values, numCols) {
var numRows = values.length / numCols;
for (var i = 0, length = values.length; i < length; i++) {
var row = (i % length / numCols) << 0; // = Math.floor
var col = i % numCols;
console.log(row, col);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment