Last active
July 22, 2019 09:32
-
-
Save bartwttewaall/efe84da770656e6b7689478be94d95d7 to your computer and use it in GitHub Desktop.
Traverse a 2d array in single for-loop
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
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(i, row, col); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment