Skip to content

Instantly share code, notes, and snippets.

@nodech
Created May 10, 2014 18:27
Show Gist options
  • Save nodech/a21d166914c647927ee1 to your computer and use it in GitHub Desktop.
Save nodech/a21d166914c647927ee1 to your computer and use it in GitHub Desktop.
snail = function(arr) {
var top, bottom, left, right;
if (arr.length === 0) return [];
if (arr.length === 1) return arr[0];
top = arr.shift();
bottom = arr.pop().reverse();
right = [];
left = [];
arr.forEach(function (elem) {
right.push(elem.pop());
left.unshift(elem.shift());
});
return [].concat(top, right, bottom, left, snail(arr));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment