Skip to content

Commit

Permalink
[Matrix] StephenGrider#14 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett Dawidowski committed Apr 4, 2019
1 parent 881f793 commit 6bd8e49
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion exercises/matrix/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,60 @@
// [11, 16, 15, 6],
// [10, 9, 8, 7]]

function matrix(n) {}
function matrix(n) {
const results = [];

// Precreate all the empty subarray
for (let i = 0; i < n; i++){
results.push([]);
}

// Global Counter Variable
let counter = 1;

// Start will always at 0
let startCol = 0;
let startRow = 0;

// End will always start at n - 1
let endCol = n - 1;
let endRow = n - 1;

while (startCol <= endCol && startRow <= endRow) {

// Building top row
for (let i = startCol; i <= endCol; i++){
results[startCol][i] = counter;
counter++;
}
startRow++;

// Building far right col
for (let i = startRow; i <= endRow; i++){
results[i][endRow] = counter;
counter++;
}
endCol--;

// Building bottom row
for (let i = endCol; i >= startCol; i--){
results[endRow][i] = counter;
counter++;
}
endRow--;

// Building far left col
for (let i = endRow; i >= startRow; i--){
results[i][startCol] = counter;
counter++;
}
startCol++;



}

return results;
}

module.exports = matrix;

0 comments on commit 6bd8e49

Please sign in to comment.