Skip to content

Commit 187ea39

Browse files
Add transpose function for a 2D array
Implemented a function to transpose a 2D array.
1 parent 08d8c6b commit 187ea39

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//It's just for Accessing the elements
2+
// const row=2;
3+
// const column=3;
4+
5+
// const matrix=[];
6+
// let count=1;
7+
// for(let i=0;i<row;i++){
8+
// const row=[];
9+
// for(let j=0;j<column;j++){
10+
// row[j]=count;
11+
// count++;
12+
// }
13+
// matrix[i]=row;
14+
// }
15+
// console.log(matrix);
16+
17+
18+
//Transpose of a matrix
19+
let arr=[[1,2,3],[3,2,1]];
20+
21+
let result=[];
22+
for(let i=0;i<arr[0].length;i++){
23+
result[i]=[];
24+
for(let j=0;j<arr.length;j++){
25+
result[i][j]=arr[j][i];
26+
}
27+
}
28+
console.log(result);

0 commit comments

Comments
 (0)