forked from bahmutov/count-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
195 lines (171 loc) · 4.83 KB
/
app.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// @ts-check
;(function () {
const { app } = window.hyperapp
const { div, span, button, footer, aside } = window.hyperappHtml
const _ = window._
const range = (from, to) => {
const a = []
for (let k = from; k <= to; k += 1) {
a.push(k)
}
return a
}
const isOdd = a => Math.abs(a % 2) === 1
const problemText = state => `${state.a}${state.op}${state.b}`
const state = {
// ranges of answers
min: -20,
max: 20,
// current problem
a: 0,
b: 0,
op: '-',
problem: '0-0',
expectedAnswer: 0,
// game state
correct: 0,
disabledAnswers: {},
language: 'ру',
rightAnswer: null
}
const switchLanguage = language => (language === 'ру' ? 'en' : 'ру')
const heroMessage = language => (language === 'ру' ? 'счет' : 'count')
const pickAddition = (min, max) => {
const a = _.random(0, max)
const b = _.random(max - a)
return { a, b }
}
const pickSubtraction = (min, max) => {
const a = _.random(0, max)
const b = _.random(0, max)
return { a, b }
}
const pickDivisionBy2 = (min, max) => {
let a = _.random(1, max)
if (isOdd(a)) {
a -= 1
}
return { a, b: 2 }
}
const pickDivisionBy3 = (min, max) => {
const b = _.random(1, 6)
const a = b * 3
return { a, b }
}
// returns true or false
const coinToss = () => _.random(0, 1) === 0
const pickNumbers = (op, min, max) => {
switch (op) {
case '+':
return pickAddition(min, max)
case '-':
return pickSubtraction(min, max)
case '/':
return coinToss()
? pickDivisionBy2(min, max)
: pickDivisionBy3(min, max)
default:
throw new Error(`Cannot pick numbers for op ${op}`)
}
}
const actions = {
load: () => state => ({
language: localStorage.getItem('language') || 'ру',
correct: parseInt(localStorage.getItem('correct') || '0')
}),
save: () => state => {
localStorage.setItem('language', state.language)
localStorage.setItem('correct', state.correct)
},
toggleOp: () => state => {
switch (state.op) {
case '+':
return { op: '-' }
case '-':
return { op: '/' }
case '/':
return { op: '+' }
}
},
// could be done nicely with merge function
setNextQuestion: ({ a, b, problem, expectedAnswer, op }) => state => ({
a,
b,
op,
problem,
expectedAnswer,
disabledAnswers: {},
rightAnswer: null
}),
nextQuestion: () => (state, actions) => {
const op = state.op
const { a, b } = pickNumbers(op, state.min, state.max)
const problem = problemText({ a, b, op })
const expectedAnswer = eval(problem)
return actions.setNextQuestion({ a, op, b, problem, expectedAnswer })
},
rightAnswer: answer => state => {
return {
correct: state.correct + 1,
rightAnswer: answer
}
},
getState: () => state => state,
answer: answer => (state, actions) => {
if (state.rightAnswer !== null) {
// we have already answered this question
// ignore multiple clicks on the same answer
return
}
if (state.expectedAnswer === answer) {
setTimeout(actions.nextQuestion, 2000)
actions.rightAnswer(answer)
actions.toggleOp()
return actions.save()
}
const disabledAnswers = state.disabledAnswers
disabledAnswers[String(answer)] = true
return { disabledAnswers }
},
language: () => state => {
language = switchLanguage(state.language)
return { language }
}
}
const correctAnswers = (language, n) =>
(language === 'ру' ? `правильно ${n}` : `correct ${n}`)
const view = (state, actions) => {
const answers = range(state.min, state.max).map(k => {
const attributes = {}
if (state.disabledAnswers[String(k)]) {
attributes.disabled = 'disabled'
} else {
attributes.onclick = actions.answer.bind(null, k)
}
return button(attributes, String(k))
})
const problemAttributes = state.rightAnswer === null
? { class: 'problem' }
: { class: 'problem right' }
const problem = state.rightAnswer === null
? span(state.problem)
: span(`${state.problem} = ${state.rightAnswer}`)
return div({ oncreate: actions.load }, [
div({ class: 'hero' }, heroMessage(state.language)),
div(problemAttributes, problem),
div({ class: 'answers' }, answers),
footer(correctAnswers(state.language, state.correct)),
aside(
{
class: 'language',
onclick: () => {
actions.language()
actions.save()
}
},
state.language
)
])
}
window.app = app(state, actions, view, document.getElementById('app'))
})()