-
Notifications
You must be signed in to change notification settings - Fork 0
/
assumption-engine.js
44 lines (39 loc) · 1.12 KB
/
assumption-engine.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
(function () {
function fallback(message) {
document.documentElement.className = 'no-js';
console && console.log('Something went wrong: ', message);
}
try {
fetch('assumptions.json')
.then(res => {
if (res.ok) {
return res.json();
}
throw new Error('Bad network response.');
})
.then(json => {
const assumptions = json.assumptions || false;
if (!assumptions) {
throw new Error('No assumptions.');
}
let assClone = [];
const assEl = document.getElementById('told-you');
const form = document.querySelector('form');
form.addEventListener('submit', (ev) => {
ev.preventDefault();
if (!assClone.length) {
assClone = assumptions.slice(0);
}
const item = Math.floor(Math.random() * assClone.length);
const assumption = assClone[item];
assClone.splice(item, 1);
assEl.innerHTML = assumption;
});
})
.catch(err => {
fallback(err.message);
});
} catch (err) {
fallback(err.message);
}
}());