-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestable.js
More file actions
143 lines (125 loc) · 4.56 KB
/
timestable.js
File metadata and controls
143 lines (125 loc) · 4.56 KB
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
// Made with no jQuery
// Usage example:
// multiplicationTable(12, 'myEl');
// Parameter 1: Number of rows and columns. Default 9.
// Parameter 2: ID of target element. Default target element is body.
/*global $,console*/
/*jslint plusplus: true, white: true, vars: true*/
//"true: white" removes indentation requirements. this is an issue with "case"
//"vars: true" removes requirement for only one var statment per function
function multiplicationTable (size, targEl) {
var i, j, tbl, tr = [], e,
lightblue = '#59C',
offBackground = '#eee',
onBackground = '#fff',
offForeground = '#37A',
onForeground = '#000',
operation;
console.log(targEl);
// Default size is 9x9 if not specified
size = (typeof size === 'undefined') ? 9 : size;
targEl = (typeof targEl === 'undefined') ?
document.body :
document.getElementById(targEl);
tbl = document.createElement('table');
tbl.style.borderCollapse = 'collapse';
tbl.style.borderSpacing = '0px';
targEl.appendChild(tbl);
targEl.style.border = '8px solid' + lightblue;
// set width to div contents
targEl.style.display = 'inline-block';
operation = document.createElement('div');
operation.innerHTML = 'Addition';
operation.style.color = offForeground;
operation.style.width = '6em';
operation.style.margin = '0px auto';
operation.style.fontFamily = 'Arial, Helvetica, sans-serif';
operation.style.fontSize = '0.9em';
operation.style.color = offForeground;
operation.style.border = '4px solid ' + lightblue;
operation.style.backgroundColor = offBackground;
function cell() {
var c = [],
q = i + 1 + '×' + (j + 1),
a = (i + 1) * (j + 1),
id = i + 1 + '-' + (j + 1);
c[i] = document.createElement('td');
c[i].innerHTML = q;
c[i].id = id;
c[i].style.fontFamily = 'Arial, Helvetica, sans-serif';
c[i].style.textAlign = 'center';
c[i].style.fontSize = '0.9em';
c[i].style.color = offForeground;
c[i].style.width = '2.5em';
c[i].style.height = '2.5em';
c[i].style.border = '4px solid ' + offForeground;
c[i].style.backgroundColor = offBackground;
c[i].style.padding = '0px';
c[i].style.margin = '0px';
c[i].onmouseover = function () {
this.innerHTML = a;
this.style.color = onForeground;
this.style.backgroundColor = onBackground;
};
c[i].onmouseout = function () {
this.innerHTML = q;
this.style.color = offForeground;
this.style.backgroundColor = offBackground;
};
tr[j].appendChild(c[i]);
}
function changeCell() {
var c = [], q, a, id;
id = i + 1 + '-' + (j + 1);
c[i] = document.getElementById(id);
console.log(c[i].id);
if (operation.innerHTML === 'Subtraction') {
q = i + 1 + '+' + (j + 1);
a = (i + 1) + (j + 1);
} else if (operation.innerHTML === 'Multiplication') {
if ((i + 1) - (j + 1) > -1) {
q = i + 1 + '-' + (j + 1);
a = (i + 1) - (j + 1);
} else {
q = j + 1 + '-' + (i + 1);
a = (j + 1) - (i + 1);
}
} else {
q = i + 1 + '×' + (j + 1);
a = (i + 1) * (j + 1);
}
c[i].innerHTML = q;
c[i].onmouseover = function () {
this.innerHTML = a;
this.style.color = onForeground;
this.style.backgroundColor = onBackground;
};
c[i].onmouseout = function () {
this.innerHTML = q;
this.style.color = offForeground;
this.style.backgroundColor = offBackground;
};
}
targEl.parentNode.insertBefore(operation, targEl.nextSibling);
for(j=0;j<size;j++){
tr[j] = document.createElement('tr');
tbl.appendChild(tr[j]);
for(i=0;i<size;i++){
cell();
}
}
operation.onclick = function () {
if (this.innerHTML === 'Addition') {
this.innerHTML = 'Subtraction';
} else if (this.innerHTML === 'Subtraction') {
this.innerHTML = 'Multiplication';
} else {
this.innerHTML = 'Addition';
}
for(j=0;j<size;j++){
for(i=0;i<size;i++){
changeCell();
}
}
};
}