Last active
August 7, 2018 14:50
-
-
Save renatocassino/93e174775d239aff35ce63e91cc285be to your computer and use it in GitHub Desktop.
getEmptyBlocks.js
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
const getEmptyBlocks = (board) => ( | |
board.reduce((lineData, line, idxColumn) => { // Iterate over columns | |
const result = line.reduce((data, row, idx) => { // Iterate over lines | |
if (row !== 0) return data; // If value is different of zero, response last iterate | |
return [[idxColumn, idx], ...data]; // Join last iterate (or default value) with new data | |
}, []); // Default value to reduce in line | |
return [...lineData, ...result]; // Join old value with new value and return to next reduce | |
}, []) // Default value to reduce in column | |
); | |
const board = [ | |
[2, 2, 2, 2], | |
[0, 4, 16, 16], | |
[8, 8, 0, 32], | |
[0, 0, 0, 64] | |
]; | |
console.log(getEmptyBlocks(board)); | |
// Response [ [ 1, 0 ], [ 2, 2 ], [ 3, 2 ], [ 3, 1 ], [ 3, 0 ] ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment