REACTO - Spiral
Problem Create a function that takes a multi-dimensional array of integer as input. From the top left, spiral traverse each element in a clockwise order. Return the spiral traversed integers in a single array.
Example:
var array =
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]];
console.log(spiral(array));
//output should be [1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10];
Solutions:
function spiral(array) {
let result = [];
while (array.length) {
// Steal the first row.
result = result.concat(array.shift());
// Steal the right items.
for (var i = 0; i < array.length; i++) result.push(array[i].pop());
// Steal the bottom row.
result = result.concat((array.pop() || []).reverse());
// Steal the left items.
for (var i = array.length - 1; i >= 0; i--) result.push(array[i].shift());
}
return result;
}
//~~~~~~~~~~~~~~~~ALTERNATE SOLUTION ~~~~~~~~~~~~~~~~~~~
// function spiral(array) {
// //if the array has a length of 1, return array
// if(array.length == 1) {
// return array[0];
// }
// var firstRow = array[0],
// numRows = array.length,
// nextRow = [],
// newArr,
// rowIndex,
// colIndex = array[1].length - 1;
// //store elements in new arrays to push into the next row
// for(colIndex; colIndex >= 0; colIndex--) {
// newArr = [];
// for(rowIndex = 1; rowIndex < numRows; rowIndex++) {
// newArr.push( array[rowIndex][colIndex]);
// }
// nextRow.push( newArr );
// }
// firstRow.push.apply( firstRow, spiral(nextRow));
// return firstRow;
// }
//~~~~~~~~~~~~~~~~ALTERNATE SOLUTION ~~~~~~~~~~~~~~~~~~~
// function spiral (array) {
// var res = [];
// while(array.length) {
// res = res.concat(array.shift())
// array = expand(array);
// }
// return res;
// }
// function expand(matrix){
// return matrix.reduce(function(res, arr, i){
// arr.forEach(function(n, j){
// if (!res[j]) res[j] = [];
// res[j][i] = n;
// })
// return res;
// }, []).reverse();
// }