-
Notifications
You must be signed in to change notification settings - Fork 153
/
rotateTile.js
50 lines (43 loc) · 1.11 KB
/
rotateTile.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @author MadhavBahl
* @date 23/01/2019
* Method -- Creating a new matrix to represent the rotated tile
*/
function rotateTile (arr) {
let n = arr.length;
// print the original tile
console.log ('Original Tile: ');
let toPrint = '';
for (let array of arr) {
toPrint = '';
for (let element of array) {
toPrint += element + ' ';
}
console.log (toPrint);
}
// Make another tile to store the rotated tile
let rotatedTile = [];
// Initialize with zeros
for (let i=0; i<n; i++) {
let row = [];
for (let j=0; j<n; j++) {
row.push(0);
}
rotatedTile.push (row);
}
for (let i=0; i<n; i++) {
for (let j=0; j<n; j++)
rotatedTile [i][j] = arr[(n-j)-1][i];
}
// print the rotated tile
console.log ('Rotated Tile: ');
for (let array of rotatedTile) {
toPrint = '';
for (let element of array) {
toPrint += element + ' ';
}
console.log (toPrint);
}
return rotatedTile;
}
rotateTile ([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);