Skip to content

Instantly share code, notes, and snippets.

@ccnixon
Last active December 12, 2015 17:21
Show Gist options
  • Save ccnixon/f8ee3e30e355435da087 to your computer and use it in GitHub Desktop.
Save ccnixon/f8ee3e30e355435da087 to your computer and use it in GitHub Desktop.
Write a function that accepts a 2-dimensional array (that is, an array containing many same-length arrays), and prints out every value found, but in a spiral from the upper left in to the center.
var matrix =
[ [ 1, 2, 3, 4, 5 ],
[ 6, 7, 8, 9, 10 ],
[ 11, 12, 13, 14, 15 ],
[ 16, 17, 18, 19, 20 ],
[ 21, 22, 23, 24, 25 ] ];
var spiralTraversal = function(matrix){
var results =[];
var startRowIndex = 0;
var endRowIndex = matrix.length-1;
var startColIndex = 0;
var endColIndex = matrix[0].length - 1;
while(startRowIndex <= endRowIndex && startColIndex <= endColIndex){
for(var i = startColIndex; i <= endColIndex; i++){
results.push(matrix[startRowIndex][i]);
}
startRowIndex++;
for(var j = startRowIndex; j <= endRowIndex; j++){
results.push(matrix[j][endColIndex]);
}
endColIndex--;
for(var k = endColIndex; k >= startColIndex; k--){
results.push(matrix[endRowIndex][k]);
}
endRowIndex--;
for(var m = endRowIndex; m >= startRowIndex; m--){
results.push(matrix[m][startColIndex]);
}
startColIndex++;
}
return results;
}
//[ 1,2,3,4,5,10,15,20,25,24,23,22,21,16,11,6,7,8,9,14,19,18,17,12,13 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment