Skip to content

Instantly share code, notes, and snippets.

@cutty853
Last active February 19, 2019 22:09
Show Gist options
  • Save cutty853/5892b07b2f934911124c32f34bac5746 to your computer and use it in GitHub Desktop.
Save cutty853/5892b07b2f934911124c32f34bac5746 to your computer and use it in GitHub Desktop.
Algorithm to tile an array. Written in NodeJS
/**
* Produce a list of list by tiling the originalArray.
*
* @param {int} tileAmount amount of tiles per ligne
* @param {Array} originalArray the array to tile
*/
function tiler(tileAmount, originalArray) {
tiles = [];
for (let index = 0; index < originalArray.length; index++) {
const element = originalArray[index];
if (index % tileAmount == 0) {
tiles.push([]);
}
tiles[Math.floor(index / tileAmount)].push(element);
}
return tiles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment