-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
85 lines (83 loc) · 2.87 KB
/
main.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
/**Alina Ryan
* 12/8/2020
* UMass Lowell Computer Science
* Assignment 5 COMP.4610
* Description: This page takes numbers
* entered into a form and generates a
* multiplication table.
***/
// var constants
const myForm = document.querySelector('#my-form');
const minCol = document.querySelector('#minCol');
const maxCol = document.querySelector('#maxCol');
const minRow = document.querySelector('#minRow');
const maxRow = document.querySelector('#maxRow');
const msg = document.querySelector('.msg');
const numList = document.querySelector('#numbers');
const btn = document.querySelector('.button');
var table=document.getElementById('mytable');
document.getElementById('mytable').style.position = 'absolute';
var output="";
btn.addEventListener('click', (e) => {
e.preventDefault();
// Check if all fields are entered
if (minCol.value === '' || maxCol.value === '' || minRow.value === ''
|| maxRow.value === '') {
msg.innerHTML = 'Please enter every field';
// error message goes away after 5 seconds
setTimeout(() => msg.remove(), 5000);
return;
}
// Check for values greater than 50
if (minCol.value > 50 || maxCol.value > 50 || minRow.value > 50 || maxRow.value > 50) {
msg.innerHTML =
'One or more of the values exceeds the limit.<br>Please make sure the values are between -50 and 50.';
setTimeout(() => msg.remove(), 5000);
return;
}
// Check for values less than -50
if (minCol.value < -50 || maxCol.value < -50 || minRow.value < -50 || maxRow.value < -50) {
msg.innerHTML =
'One or more of the values exceeds the limit.<br>Please make sure the values are between -50 and 50.';
setTimeout(() => msg.remove(), 5000);
return;
}
// Swap values if start is greater than end
if (minCol.value > maxCol.value) {
var temp = minCol.value;
minCol.value = maxCol.value;
maxCol.value = temp;
msg.innerHTML =
'Swapping minCol and maxCol';
setTimeout(() => msg.remove(), 5000);
}
if (minRow.value > maxRow.value) {
var temp = minRow.value;
minRow.value = maxRow.value;
maxRow.value = temp;
msg.innerHTML =
'Swapping minRow and maxRow.';
setTimeout(() => msg.remove(), 5000);
}
///////////// Generate Table ////////////////
else {
for (var j = minCol.value - 1; j <= maxCol.value; j++) {
output += "<tr>";
if (j == minCol.value - 1) {
output += "<td></td>"; // empty cell
for (i = minRow.value; i <= maxRow.value; i++) {
output += "<td>" + i + "</td>";
}
} else {
output += "<td>" + j + "</td>";
for (i = minRow.value; i <= maxRow.value; i++) {
output += "<td>" + i * j + "</td>";
}
}
output += "</tr>";
}
// Insert table
table.innerHTML = output;
}
});