-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpassages.js
More file actions
48 lines (40 loc) · 1.41 KB
/
passages.js
File metadata and controls
48 lines (40 loc) · 1.41 KB
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
const YEAR = 2025,
passageCache = {}
export async function fetchPassages(division, translation) {
const key = `${division.toLowerCase()}-${translation.toLowerCase()}`
let passages = passageCache[key]
if (passages === undefined) {
passages = await (await fetch(`${YEAR}/${key}.json`, {method: 'GET'})).json()
passageCache[key] = passages
}
return passages
}
export function choosePassages(passages, count, totalMinutes, targetWordsPerMinute) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const minRate = targetWordsPerMinute - 7.5,
maxRate = targetWordsPerMinute + 7.5
let attempt, words, wpm
for (attempt = 0; attempt < 1000000; attempt++) {
const picked = pick(passages, count)
words = picked.reduce((total, p) => (total + p.word_count), 0)
wpm = words / totalMinutes
if (minRate <= wpm && wpm <= maxRate) {
console.log(`${count} passages chosen: words = ${words}, wpm = ${wpm}`)
resolve(picked)
return
}
}
reject('Maximum attempts reached. Please adjust parameters and try again.')
}, 0)
})
}
function pick(items, count) {
const indices = Array.from(items, (_, i) => i),
chosen = []
while (indices.length && chosen.length < count) {
const i = Math.floor(Math.random() * indices.length)
chosen.push(items[indices.splice(i, 1)[0]])
}
return chosen
}