-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (168 loc) · 5.57 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var matrix1Rows = 3;
var matrix1Cols = 3;
var matrix2Rows = 3;
var matrix2Cols = 3;
document.getElementById("multiplyABButton").onclick = function()
{
document.getElementById("outputMessage").innerHTML = "";
document.getElementById("outputMatrix").innerHTML = "";
if (matrix1Cols != matrix2Rows)
{
document.getElementById("outputMessage").innerHTML = "Incompatible Matrix Dimensions";
return;
}
addColumn('outputMatrix', 'outputMatrixRow', 'o', 0, 0);
for (var outputCols = 2; outputCols <= matrix2Cols; outputCols++)
{
addColumn('outputMatrix', 'outputMatrixRow', 'o', outputCols, 1);
}
for (var outputRows = 2; outputRows <= matrix1Rows; outputRows++)
{
addRow('outputMatrix', 'outputMatrixRow', 'o', matrix2Cols, outputRows);
}
for (var x = 1; x <= matrix2Cols; x++)
{
for (var y = 1; y <= matrix1Rows; y++)
{
document.getElementById('o' + x + y).readOnly = true;
}
}
var o;
//we don't use 0, for better readability
var a = new Array(matrix1Cols+1);
var b = new Array(matrix2Rows+1);
for (var row = 1; row <= matrix1Rows; row++)
{
for (var col = 1; col <= matrix2Cols; col++)
{
o = document.getElementById("o" + row + col);
for (var i = 1; i <= matrix1Cols; i++)
{
a[i] = parseInt(document.getElementById("a" + row + i).value);
}
for (var i = 1; i <= matrix2Rows; i++)
{
b[i] = parseInt(document.getElementById("b" + i + col).value);
}
var oValue = 0;
for (var i = 1; i <= matrix1Cols; i++)
{
oValue += a[i] * b[i];
}
o.value = oValue;
}
}
}
function addRow(matrix_name, row_name, element_name, colsize, rowNumber)
{
if (colsize == 0 && rowNumber == 0)
{
var firstElement = document.createElement('div');
firstElement.id = row_name + 1;
var input = document.createElement('input');
input.type = 'text';
input.id = element_name + 1 + 1;
input.size = '1';
firstElement.appendChild(input);
firstElement.insertAdjacentText('beforeend', ` `);
document.getElementById(matrix_name).appendChild(firstElement);
return;
}
var newRow = document.createElement('div');
newRow.id = row_name + rowNumber;
for (var i = 1; i <= colsize; i++)
{
var input = document.createElement('input');
input.type = 'text';
input.id = element_name + rowNumber + i;
input.size = '1';
newRow.appendChild(input);
newRow.insertAdjacentText('beforeend', ` `);
}
document.getElementById(matrix_name).appendChild(newRow);
}
function removeRow(matrix_name, row_name, rowNumber)
{
var targetID = row_name + rowNumber;
var targetChild = document.getElementById(targetID);
document.getElementById(matrix_name).removeChild(targetChild);
}
function addColumn(matrix_name, row_name, element_name, colsize, rowNumber)
{
if (colsize == 0 && rowNumber == 0)
{
var firstElement = document.createElement('div');
firstElement.id = row_name + 1;
var input = document.createElement('input');
input.type = 'text';
input.id = element_name + 1 + 1;
input.size = '1';
firstElement.appendChild(input);
firstElement.insertAdjacentText('beforeend', ` `);
document.getElementById(matrix_name).appendChild(firstElement);
return;
}
for (var i = 1; i <= rowNumber; i++)
{
var input = document.createElement('input');
input.type = 'text';
input.id = element_name + i + colsize;
input.size = '1';
document.getElementById(row_name + i).appendChild(input);
document.getElementById(row_name + i).insertAdjacentText('beforeend', ` `);
}
}
function removeColumn(row_name, element_name, colsize, rowNumber)
{
for (var i = 1; i <= rowNumber; i++)
{
var target = document.getElementById(element_name + i + colsize);
document.getElementById(row_name + i).removeChild(target);
}
}
document.getElementById("incRow1").onclick = function()
{
matrix1Rows++;
addRow('matrix1', 'matrix1Row', 'a', matrix1Cols, matrix1Rows);
}
document.getElementById("decRow1").onclick = function()
{
if (matrix1Rows > 0)
{
removeRow('matrix1', 'matrix1Row', matrix1Rows);
matrix1Rows--;
}
}
document.getElementById("incCol1").onclick = function()
{
matrix1Cols++;
addColumn('matrix1', 'matrix1Row', 'a', matrix1Cols, matrix1Rows);
}
document.getElementById("decCol1").onclick = function()
{
removeColumn('matrix1Row', 'a', matrix1Cols, matrix1Rows);
matrix1Cols--;
}
document.getElementById("incRow2").onclick = function()
{
matrix2Rows++;
addRow('matrix2', 'matrix2Row', 'b', matrix2Cols, matrix2Rows);
}
document.getElementById("decRow2").onclick = function()
{
if (matrix2Rows > 0)
{
removeRow('matrix2', 'matrix2Row', matrix2Rows);
matrix2Rows--;
}
}
document.getElementById("incCol2").onclick = function()
{
matrix2Cols++;
addColumn('matrix2', 'matrix2Row', 'b', matrix2Cols, matrix2Rows);
}
document.getElementById("decCol2").onclick = function()
{
removeColumn('matrix2Row', 'b', matrix2Cols, matrix2Rows);
matrix2Cols--;
}