-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratchpad.js
182 lines (158 loc) · 4.44 KB
/
scratchpad.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
function fibFunction(n) {
var value;
var div = document.createElement('div');
div.setAttribute("class", "fib");
n = parseInt(n);
if (n < 2) {
if (n === 0) {
value = 0;
} else if (n === 1) {
value = 1;
}
var p = document.createElement('p');
p.textContent = 'Fib(' + n + ') = ' + value;
div.appendChild(p)
} else {
var left = fibFunction(n - 1);
var clas = left.html.getAttribute('class');
left.html.setAttribute('class', clas + ' fib-left');
var right = fibFunction(n - 2);
clas = right.html.getAttribute('class');
right.html.setAttribute('class', clas + ' fib-right');
value = left.value + right.value;
var p = document.createElement('p');
p.textContent = 'Fib(' + n + ') = ' + value;
div.appendChild(p);
div.appendChild(left.html);
div.appendChild(right.html);
}
return { 'value': value, 'html': div };
}
function pellFunction(n) {
var seq;
var div = document.createElement('div');
div.setAttribute("class", "fib");
n = parseInt(n);
if(n < 2) {
switch (n) {
case 0:
seq = 0;
break;
case 1:
seq = 1;
break;
}
var p = document.createElement('p');
p.textContent = 'Pell(' + n + ') = ' + seq;
div.appendChild(p)
} else {
var left = pellFunction(n - 1);
var temp = left.html.getAttribute("class");
left.html.setAttribute("class", temp + " fib-left");
var right = pellFunction(n - 2);
temp = right.html.getAttribute("class");
right.html.setAttribute("class", temp + " fib-right");
seq = (2 * left.seq) + right.seq;
var p = document.createElement('p');
p.textContent = 'Pell(' + n + ') = ' + seq;
div.appendChild(p);
div.appendChild(left.html);
div.appendChild(right.html);
}
return { 'seq': seq, 'html': div };
}
function tribFunction(n) {
var seq;
var div = document.createElement('div');
div.setAttribute("class", "fib");
n = parseInt(n);
if (n < 3) {
switch (n) {
case 0:
case 1:
seq = 0;
break;
case 2:
seq = 1;
break;
}
var p = document.createElement('p');
p.textContent = 'Tribonacci(' + n + ') = ' + seq;
div.appendChild(p)
} else {
var left = tribFunction(n - 1);
var clas = left.html.getAttribute("class");
left.html.setAttribute("class", clas + " fib-left");
var mid = tribFunction(n - 2);
clas = mid.html.getAttribute("class");
mid.html.setAttribute("class", clas + " fib-mid");
var right = tribFunction(n - 3);
clas = right.html.getAttribute("class");
right.html.setAttribute("class", clas + " fib-right");
seq = left.seq + mid.seq + right.seq;
var p = document.createElement('p');
p.textContent = 'Tribonacci(' + n + ') = ' + seq;
div.appendChild(p);
div.appendChild(left.html);
div.appendChild(mid.html);
div.appendChild(right.html);
}
return { 'seq': seq, 'html': div };
}
var fib = function (n, node) {
var fibTree = node.querySelector('div.fib');
if (fibTree) {
node.removeChild(fibTree);
}
var tree = fibFunction(n);
node.appendChild(tree.html);
}
var pell = function (n, node) {
var fibTree = node.querySelector('div.fib');
if (fibTree) {
node.removeChild(fibTree);
}
var tree = pellFunction(n);
node.appendChild(tree.html);
}
var trib = function (n, node) {
var fibTree = node.querySelector('div.fib');
if (fibTree) {
node.removeChild(fibTree);
}
var tree = tribFunction(n);
node.appendChild(tree.html);
}
var fibButton = function(me) {
var form = me.parentNode;
var slider = form.querySelector('input');
var value = slider.value;
fib(value, form.parentNode);
}
var fibSlider = function(me) {
var form = me.parentNode;
var button = form.querySelector('button');
button.textContent = 'Fib(' + me.value + ')';
}
var pellButton = function(me) {
var form = me.parentNode;
var slider = form.querySelector('input');
var value = slider.value;
pell(value, form.parentNode);
}
var pellSlider = function(me) {
var form = me.parentNode;
var button = form.querySelector('button');
button.textContent = 'Pell(' + me.value + ')';
}
var tribButton = function(me) {
var form = me.parentNode;
var slider = form.querySelector('input');
var value = slider.value;
trib(value, form.parentNode);
}
var tribSlider = function(me) {
var form = me.parentNode;
var button = form.querySelector('button');
button.textContent = 'Trib(' + me.value + ')';
}