Skip to content

Commit

Permalink
Complete rotateMatrix.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 1, 2015
1 parent 6ddaec9 commit c6fa6d4
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions rotateMatrix/rotateMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,31 @@
* - Make your function accept a parameter for the direction of rotation (1 = clockwise, -1 = counterclockwise)
*/

var rotateMatrix = function (matrix) {
};
var rotateMatrix = function (matrix, direction) {
direction = direction || 1;
if ( typeof direction !== 'number' || (direction !== 1 && direction !== -1) ) {
throw new Error('direction must be 1 for clockwise or -1 for counterclockwise');
}

var rows = matrix.length;
var cols = matrix[0].length;
var newRows = cols;
var newCols = rows;
var result = [];
var i, j, k;

// give result matrix the correct number of rows
for (k = 0; k < cols; k += 1) {
result.push( [] );
}

for (i = 0; i < newRows; i += 1) {
for (j = 0; j < newCols; j += 1) {
result[i][j] = direction === 1 ?
matrix[newCols - j - 1][i] :
matrix[j][newRows - i - 1];
}
}

return result;
};

0 comments on commit c6fa6d4

Please sign in to comment.