Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  4. const expected = [0, 4, 8, 1, 5, 9, 2, 6, 3, 7];
  5.  
  6. function transpose(arr, columns) {
  7. const rows = Math.ceil(arr.length / columns);
  8.  
  9. return arr.reduce((result, v, i) => {
  10. const col = Math.ceil((i + 1) / rows);
  11. const row = i % rows;
  12.  
  13. result[row * columns + col - 1] = v;
  14.  
  15. return result;
  16. }, []).filter(v => typeof v != 'undefined');
  17. }
  18.  
  19. function equals(a, b) {
  20. return JSON.stringify(a) == JSON.stringify(b);
  21. }
  22.  
  23. const result = transpose(source, 3);
  24.  
  25. console.log(result, equals(result, expected));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement