-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpi.html
56 lines (45 loc) · 1.74 KB
/
pi.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
<!DOCTYPE html>
<html>
<head>
<title>Pi Calculator - Online</title>
</head>
<body>
<h1>Pi Calculator - Online</h1>
<form method="post" action="" onsubmit="test(this.digits.value);return false;">
Number of Digits:
<input type="text" name="digits" value="800" size="25" maxlength="25">
<br>
<button onclick="test(this.form.digits.value);">Calculate Pi</button>
<h4>Output:</h4>
<div id="log"></div>
<mark>
<b id="pi"></b>
</mark>
</form>
<script src="pi.js" type="text/javascript"></script>
<script type="text/javascript">
const elm_log = document.querySelector("#log")
const elm_pi = document.querySelector("#pi")
const chunk = (str, n) => {
const chunks = []
const len = str.length
for(let i = 0; i < len; i += n)
chunks.push(str.substr(i, n))
return chunks
}
const test = (digits) => {
let res = calculate(digits)
elm_log.innerHTML = `Calculate ${res.digits} digits of PI number in ${res.time} seconds.<br><br>PI(${res.digits}) = 3.<br>\n`
// elm_pi.innerText = res.pi
// elm_pi.innerHTML = chunk(res.pi, 50).join(" (50) <br>").trimEnd("(50) <br>")
elm_pi.innerHTML = chunk(res.pi.substr(1), 50)
.map(line =>
chunk(line, 5).join(" ")
).join("<br>")
}
window.addEventListener("load", () => {
document.querySelector("button").click()
})
</script>
</body>
</html>