Created
August 26, 2016 15:29
-
-
Save damirm/b191099d95f04dd19c48d083387cfaeb to your computer and use it in GitHub Desktop.
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
'use strict'; | |
const source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
const expected = [0, 4, 8, 1, 5, 9, 2, 6, 3, 7]; | |
function transpose(arr, columns) { | |
const rows = Math.ceil(arr.length / columns); | |
return arr.reduce((result, v, i) => { | |
const col = Math.ceil((i + 1) / rows); | |
const row = i % rows; | |
result[row * columns + col - 1] = v; | |
return result; | |
}, []).filter(v => typeof v != 'undefined'); | |
} | |
function equals(a, b) { | |
return JSON.stringify(a) == JSON.stringify(b); | |
} | |
const result = transpose(source, 3); | |
console.log(result, equals(result, expected)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment