-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.html
532 lines (468 loc) · 13.1 KB
/
index2.html
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<html>
<head>
<title>Online JavaScript Interpreter</title>
<script>
/*
This page contains JavaScript functions that evaluate interactive
JavaScript locally in a browser
Version of Jan 3, 2013, (c) Peter Jipsen http://www.chapman.edu/~jipsen
Latest version at http://www.chapman.edu/~jipsen/js
If you use it on a webpage, please send the URL to jipsen@chapman.edu
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License (at http://www.gnu.org/copyleft/gpl.html)
for more details.
*/
JSoutput = function(a) {
var str = "["
if (typeof(a)=="object" && a.length) {
for (var i=0; i < a.length; i++)
if (typeof(a[i])=="object" && a[i].length) {
str += (i==0?"":" ")+"["
for (var j=0; j<a[i].length; j++)
str += a[i][j]+(j==a[i].length-1?
"]"+(i==a.length-1?"]":",")+"\n":", ");
} else str += a[i]+(i==a.length-1?"]":", ");
} else str = a;
return str;
}
write = function(str) {
var outnode = document.getElementById("JSoutput");
outnode.value += JSoutput(str);
}
writeln = function(str) {
if (!str) str="";
var outnode = document.getElementById("JSoutput");
outnode.value += JSoutput(str)+"\n";
}
JSrun = function() {
console.log("haha")
var str;
var outnode = document.getElementById("JSoutput");
outnode.value = "";
d = new Date().getTime();
try {
with (Math) {
str = JSoutput(eval(document.getElementById("JSprogram").value));
}
} catch(e) {
str = e.name+" at line "+(e.lineNumber-56)+": "+e.message;
}
var tnode = document.getElementById("JStiming");
tnode.innerHTML = ""+(new Date().getTime()-d)/1000;
if (str != undefined) {outnode.value += str;}
}
JSselect = function() {
var id = document.getElementById("JSexamples").value;
var str = document.getElementById(id).value;
document.getElementById("JSprogram").value = str;
JSrun();
}
keyUp = function(event){
if (event.which==77 && event.ctrlKey) JSrun();
}
random_list = function(n, r, s) {
var a = [];
for (var i = 0; i < n; i++) {
a[i] = Math.floor(Math.random()*(s-r+1)) + r;
}
return a;
}
random_matrix = function(m, n, r, s) {
var A = [];
for (var i = 0; i < m; i++) {
A[i] = [];
for (var j = 0; j < n; j++)
A[i][j] = Math.floor(Math.random()*(s-r+1)) + r;
}
return A;
}
factorial = function(n) { // simple version
if (n <= 0) return 1;
else return n*factorial(n-1);
}
fibonacci = function(n) {
var a = 0;
var b = 1;
for (var i=0; i<n; i++) {
b = a+b;
a = b-a;
}
return a;
}
</script>
<style>td {background:white; vertical-align:top; border:1px solid black; padding:2px 10px}
pre {margin:0}
table {border-collapse:collapse; table-layout:fixed}
body {background:lightblue; font-family:Arial}
textarea {font-family:Lucida console}
</style>
</head>
<body onload="JSrun()"><!-- onpaste="return false;"-->
<b><u>Online JavaScript Interpreter:</u> Programming anywhere, nothing to install, works in any browser
</b><br/>
Type your JavaScript program into the box below. Then click the <b>Run</b>
button to see the result. To <b>save your work</b>, select the text
and copy it to an editor or email it to yourself.
<table width="100%">
<tr><td><b>Type JavaScript</b> Examples: <select id="JSexamples" onchange="JSselect()">
<option value="maximum" selected>Maximum element</option>
<option value="randomlist">Random list</option>
<option value="linear">Linear search</option>
<option value="binary">Binary search</option>
<option value="bubble">Bubble sort</option>
<option value="base">Convert base</option>
<option value="powermod">Modular exponentiation</option>
<option value="gcd">Greatest common divisor</option>
<option value="factor">Prime factorization</option>
<option value="factorial">Factorial recursion</option>
<option value="fibonacci">Fibonacci numbers</option>
<option value="combinations">Combinations</option>
<option value="randommatrix">Random matrix</option>
<option value="matrixmult">Matrix multiplication</option>
</select>
</td><td>
<input type="button" value="Run (Ctrl-m)" onclick="JSrun()"/>
<b>Output</b>
Timing: <span id="JStiming"></span> s
</td></tr>
<tr><td width="50%" style="padding:1px">
<textarea id="JSprogram" style="height:5in; width:100%; font-family: monospace; font-size: 16; font-weight: bold;" onkeyup="keyUp(event)">
// Find the maximum value in a list of numbers
max = function(a) {
var m = a[0];
for (var i = 1; i < a.length; i++) {
writeln("m = "+m);
if (m < a[i]) m = a[i];
}
return m;
}
a = random_list(6,1,10);
writeln(a);
max(a);
</textarea>
</td>
<td width="50%" style="padding:1px">
<textarea id="JSoutput" style="height:5in; width:100%; font-family: monospace; font-size: 16; font-weight: bold;">
</textarea></td>
</tr>
</table>
<b>Note that this is dynamic code running locally on your machine. If
you leave this page before copying and saving your work, it may
disappear.
</b>
<p>
<b>Quick reference to basic JavaScript commands</b>. Search online
for <a href="http://www.google.com/search?q=javascript+tutorial">tutorials</a>
<table>
<tr>
<td><b>Comment</b></td>
<td><pre>// this is a one-line comment
/* this comment could contain linebreaks */
</pre></td>
</tr>
<tr>
<td><b>Constant</b></td>
<td><tt><b>numbers</b> 12.3 <b>and strings</b> "hi", "hi".charAt(0) <b>is</b> "h"</tt></td>
</tr>
<tr>
<td><b>Variable</b></td>
<td><tt><b>start with a letter, then use letters, digits or _ (declared with</b> var<b>)</b></tt></td>
</tr>
<tr>
<td><b>Expression</b></td>
<td><tt><b>built from</b> +, -, *, /, pow(x,y), sqrt(), PI, E, log(), floor(), ceil(),</br>
random(), sin(), cos(), tan(), atan() <b>and constants, variables, functions</b></tt></td>
</tr>
<tr>
<td><b>Assignment</b></td>
<td><pre>v = expression;</pre></td>
</tr>
<tr>
<td><b>Block</b></td>
<td><pre>{ statement 1;
...
statement n;
}</pre></td>
</tr>
<tr>
<td><b>Condition</b></td>
<td><tt>expression == expression <b>or</b> <=, <, >, >=, !=...<br>
<b>or</b> !cond, cond1 && cond2, cond1 || cond2</tt></td>
</tr>
<tr>
<td><b>if - else</b></td>
<td><pre>if (condition) block 1
else block 2 // optional
</pre></td>
</tr>
<tr>
<td><b>for-loop</b></td>
<td><pre>for (var i=start; endcondition; i++)
block
</pre></td>
</tr>
<tr>
<td><b>while-loop</b></td>
<td><pre>while (condition)
block
</pre></td>
</tr>
<tr>
<td><b>Function</b></td>
<td><pre>f = function(v1, ..., vn) {
statement 1;
...
return expression;
}
</pre></td>
</tr>
<tr>
<td><b>List</b> (array)</td>
<td><pre>a = [1,2,"hi"]
a[0] is 1, a.length is 3, a+[4] is [1,2,"hi",4]</pre></td>
</tr>
<tr>
<td><b>Object</b><br/>(hash tables)</td>
<td><pre>object = {attribute:expression, method:function}
a = {x:2, y:3, s:function(){return this.x+this.y}}
a.x is 3, a["x"] is 3, a.s() is 5
</pre></td>
</tr>
<tr>
<td><b>Output</b></td>
<td><pre>write("Hello"); writeln(" world");
</pre></td>
</tr>
</table>
</p>
<p>
This page is useful for experimenting with basic JavaScript and simple
algorithms (e.g. those covered in a discrete mathematics course). It
should work in any browser that has JavaScript enabled (including
smartphone browsers).
</p>
<p>
You can save your own local copy of this page (using <b>Save As...</b>),
and use it even when you are not connected to the internet.
</p>
<textarea id="maximum" style="display:none">
// Find the maximum value in a list of numbers
max = function(a) {
var m = a[0];
for (var i = 1; i < a.length; i++) {
writeln("m = "+m);
if (m < a[i]) m = a[i];
}
return m;
}
a = random_list(6,1,10);
writeln(a);
max(a);
</textarea>
<textarea id="randomlist" style="display:none">
// Construct a list of n random integers from r to s
// random() returns a uniformly distributed random number in [0,1)
random_list = function(n, r, s) {
var a = [];
for (var i = 0; i < n; i++) {
a[i] = floor(random()*(s-r+1)) + r;
}
return a;
}
random_list(100,1,10)
</textarea>
<textarea id="linear" style="display:none">
// Find first occurrence of an element in a list of numbers
linear_search = function(x,a) {
var i = 0;
while (i < a.length && x != a[i]) {
writeln("i = "+i);
i = i+1;
}
if (i < a.length) return i;
else return "not found";
}
a = random_list(6,1,10);
writeln(a);
linear_search(3,a);
</textarea>
<textarea id="binary" style="display:none">
// Find first occurrence of an element in a *sorted* list of numbers
binary_search = function(x,a) {
var i = 0; // i is the left endpoint of the search interval
var j = a.length-1; // j is the right endpoint of the search interval
while (i < j) {
writeln("i="+i+", j="+j);
m = floor((i+j)/2);
if (x > a[m]) i = m+1;
else j = m;
}
if (x == a[i]) return i;
else return "not found";
}
binary_search(3, [1,2,2,2,3,3,4,5,6,7,8]);
</textarea>
<textarea id="bubble" style="display:none">
// Sort a list of numbers using (inefficient) bubble sort
bubble_sort = function(a) {
var t;
for (var i = 0; i < a.length-1; i++)
for (var j = 0; j < a.length-i; j++)
if (a[j] > a[j+1]) {
writeln("swap "+a[j]+", "+a[j+1]);
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
return a;
}
a = random_list(6,1,10);
writeln(a);
bubble_sort(a);
</textarea>
<textarea id="base" style="display:none">
// Convert a positive base 10 integer to another base
convert = function(n,b) {
var m = [];
while (n > 0) {
m = [n % b].concat(m);
n = floor(n/b);
}
return m;
}
convert(123456789,5);
</textarea>
<textarea id="powermod" style="display:none">
// Calculate the n-th power mod m efficiently
powermod = function(b,n,m) {
var x = 1;
var p = b % m;
while (n > 0) {
if (n%2 == 1) x = (x*p) % m;
p = (p*p) % m;
n = floor(n/2);
writeln("n = "+n+", x = "+x);
}
return x;
}
powermod(123456789, 543, 12345);
</textarea>
<textarea id="gcd" style="display:none">
// Find the greatest common divisor of two positive integers
// using the Euclidean algorithm
gcd = function(a,b) {
var r;
while (b > 0) {
writeln("a = "+a+", b = "+b);
r = a % b; // remainder
a = b;
b = r;
}
return a;
}
gcd(49,84);
//gcd(fibonacci(15),fibonacci(14));
</textarea>
<textarea id="factor" style="display:none">
// Find the prime factors of a positive integer (inefficiently)
primefactors = function(n) {
var p = 2;
var a = [];
while (p*p <= n) {
if (n%p == 0) {
n = floor(n/p);
writeln(n*p+" = "+p+" * "+n);
a = a.concat([p]);
} else p = p+1;
}
return a.concat([n]);
}
primefactors(1234567891011);
//primefactors(2*3*5*7*11*13 + 1);
//primefactors(123456789123451);
</textarea>
<textarea id="factorial" style="display:none">
// Calculate the factorial of a natural number using recursion
factorial = function(n) { // simple version
if (n <= 0) return 1;
else return n*factorial(n-1);
}
factorialw = function(n) { // write intermediate results
var k;
if (n <= 0) return 1;
else {
writeln("n = "+n);
k = n*factorialw(n-1);
writeln("k = "+k);
return k;
}
}
factorialw(10);
</textarea>
<textarea id="fibonacci" style="display:none">
// Calculate the n-th Fibonacci number iteratively
fibonacci = function(n) {
var a = 0;
var b = 1;
for (var i=0; i<n; i++) {
write(a+" ");
b = a+b;
a = b-a;
}
writeln("\n");
return a;
}
fibonacci(50);
</textarea>
<textarea id="combinations" style="display:none">
// Calculate the binomial coefficient C(n,k)
C = function(n,k) {
var c = 1;
for (var i=1; i<=k; i++) {
writeln("c = "+c);
c = c*(n-k+i)/i;
}
return c;
}
C(50,20);
</textarea>
<textarea id="randommatrix" style="display:none">
// Construct a matrix of m x n random integers from r to s
random_matrix = function(m, n, r, s) {
var A = [];
for (var i = 0; i < m; i++) {
A[i] = [];
for (var j = 0; j < n; j++)
A[i][j] = Math.floor(Math.random()*(s-r+1)) + r;
}
return A;
}
random_matrix(3,3,0,4)
</textarea>
<textarea id="matrixmult" style="display:none">
// multiply two matrices
mult = function(A,B) {
var C = [];
for (var i = 0; i = A.length; i++) {
C[i] = [];
for (var j = 0; j = B[0].length; j++) {
C[i][j] = 0;
for (var k = 0; k = A[0].length; k++)
C[i][j] = C[i][j] + A[i][k]*B[k][j];
}
}
return C;
}
A = random_matrix(3,3,0,4); writeln(A)
B = random_matrix(3,3,0,4); writeln(B)
mult(A,B)
</textarea>
<hr/>
</body>
</html>