-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathshort-sentences.js
28 lines (26 loc) · 933 Bytes
/
short-sentences.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
const choose = (a,rnd) => a[Math.floor(rnd()*a.length)]
function generateExercise(wordCount, rnd) {
let prevElements = [], element
let words = []
let charsLeft = wordCount * 5 + 1
while (charsLeft > 0) {
do {
element = choose(shortSentences, rnd)
} while(prevElements.includes(element))
if(prevElements.length > 20) prevElements.shift()
prevElements.push(element)
const string = typeof element === 'string' ? element : element.join(' ')
const array = Array.isArray(element) ? element : element.split(/\s+/)
charsLeft -= 1 + string.length
words.splice(words.length, 0, ...array)
}
return new TypeJig.Exercise(words, 0, false, 'ordered')
}
let jig
window.addEventListener('load', () => jig = loadExercisePage(args => {
const nWords = args.word_count==null ? 100 : parseInt(args.word_count)
return {
generate: (rng, options) => generateExercise(nWords, rng),
options: { name: "Short Sentences" }
}
}))