Skip to content

Instantly share code, notes, and snippets.

@lamchau
Last active May 17, 2016 02:43
Show Gist options
  • Save lamchau/a8e9d94840ae86877aa88f5394e30e1f to your computer and use it in GitHub Desktop.
Save lamchau/a8e9d94840ae86877aa88f5394e30e1f to your computer and use it in GitHub Desktop.
var input = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
var output = [
[1],
[4, 2],
[7, 5, 3],
[8, 6],
[9]
];
var columns = input[0].length;
var rows = input.length;
var result = [];
for (var index = 0; index < (rows + columns); index++) {
var i = index < rows ? index : rows - 1;
var j = index < rows ? 0 : (index - rows + 1);
while (i >= 0 && j < columns) {
result[index] = result[index] || [];
result[index].push(input[i--][j++]);
}
}
result.forEach(function(row) {
console.log(row.map(function(v) {
// if (v < 10) {
// return " " + v;
// }
return v;
}).join(" "));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment