Created
March 20, 2024 13:04
-
-
Save bartwttewaall/a0a26285d040431a6c0a86e772531e59 to your computer and use it in GitHub Desktop.
Traverse a 2-dimensional array in a single for-loop
This file contains hidden or 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
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