forked from 0xvashishth/CalcHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
79 lines (70 loc) · 1.98 KB
/
script.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
button=document.getElementById('button');
button.addEventListener('click',e=>{
let ans=document.createElement('div');
document.getElementById('matrix').appendChild(ans);
const n=3;
let j=0;
let arr=new Array(n);
let input=document.getElementsByName('array[]');
for (let i = 0; i < n; i++) {
arr[i]=[];
for(;j<(i+1)*n;j++){
let a=input[j];
if(a.value==""){
alert("Enter the values in matrix!");
return;
}
arr[i].push(a.value);
}
}
transpose(arr);
ans.innerHTML="Transpose Matrix: ";
ans.classList.add('fontstyle');
let table=document.createElement('table');
let thead=document.createElement('thead');
let tbody=document.createElement('tbody');
table.appendChild(thead);
table.appendChild(tbody);
document.getElementById('body').appendChild(table);
let trRow=document.createElement('tr');
for (let i = 1; i <= n; i++) {
let thRow=document.createElement('th');
thRow.innerHTML="A"+i;
trRow.appendChild(thRow);
}
thead.appendChild(trRow);
let trData1=document.createElement('tr');
for (let i = 0; i <n; i++) {
let num=arr[0][i];
let tdData1=document.createElement('td');
tdData1.innerHTML=""+num;
trData1.appendChild(tdData1);
}
tbody.appendChild(trData1);
let trData2=document.createElement('tr');
for (let i = 0; i <n; i++) {
let num=arr[1][i];
let tdData2=document.createElement('td');
tdData2.innerHTML=""+num;
trData2.appendChild(tdData2);
}
tbody.appendChild(trData2);
let trData3=document.createElement('tr');
for (let i = 0; i <n; i++) {
let num=arr[2][i];
let tdData3=document.createElement('td');
tdData3.innerHTML=""+num;
trData3.appendChild(tdData3);
}
tbody.appendChild(trData3);
table.classList.add('ansTableClass');
});
function transpose(arr){
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j<i; j++) {
const temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
}