-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
66 lines (53 loc) · 1.94 KB
/
script.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
window.JG = {}
JG = (function (jg) {
const problemNum = document.location.search.substr(1)
if (problemNum) {
addSrc(`${problemNum}.js`)
} else {
const title = document.createTextNode('Append ?1 to URL for my solution to https://projecteuler.net/problem=1 etc')
document.body.appendChild(title)
}
function solution (num, func, file) {
if (file) addSrc(file, run)
else run()
function run () {
// separate the comment explaining the problem from the solution
const [ code1, problemText, code2 ] = func.toString().split(/ *\/\* *START| *END *\*\/\n/m) // use /*START blah blah END*/ to hold the problem text
const codeText = [code1, code2].join('')
const title = el('h1')
title.innerHTML = `Project Euler - <a href="https://projecteuler.net/problem=${num}">Problem #${num}</a>`
document.body.appendChild(title)
const problem = el('div')
const text = problemText.split('\n').map(line => line.trim()).join('<br>').trim()
problem.innerHTML = `<p>${text}</p>`
document.body.appendChild(problem)
if (file) {
const withFile = el('p')
withFile.innerHTML = `<i>Using file <a href="${file}">${file}</a></i>`
document.body.appendChild(withFile)
}
const result = el('p')
const start = Date.now()
let evaled = func()
if (Array.isArray(evaled)) evaled = evaled.join(', ')
const timeTaken = Date.now() - start
result.innerHTML = `Result: <b>${evaled}</b> (took ${timeTaken}ms)`
document.body.appendChild(result)
const p = el('pre')
p.innerText = codeText
document.body.appendChild(p)
}
}
// can't use xhr if working on file://
function addSrc (path, cb) {
const script = el('script')
if (cb) script.onload = cb
script.src = path
document.body.appendChild(script)
}
function el (el) {
return document.createElement(el)
}
jg.solution = solution
return jg
})(JG)