-
Notifications
You must be signed in to change notification settings - Fork 0
/
DivWithRemainderChallenge.svelte
65 lines (54 loc) · 1.71 KB
/
DivWithRemainderChallenge.svelte
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
<script lang="ts">
// Presents a "division with remainder" challenge of the form: `A / B = C, D`, where C and D are the values to find.
// - A <= settings.max, B <= settings.max
// - B <= A (just to keep things interesting)
// - D >= 0 (we're considering the positive modulo only)
import { onMount } from "svelte";
import ChallengeInput from "./ChallengeInput.svelte";
import ChallengeValue from "./ChallengeValue.svelte";
import { settings, type Result, resultLog } from "./stores";
const max = settings.maxValue;
let a: number;
let b: number;
let c: number;
let d: number;
let responseInput: ChallengeInput;
onMount(next);
export function evaluate() {
const resultOk = Math.floor(a / b) === c;
const remainderOk = a % b === d;
const result = <Result>{
correct: resultOk && remainderOk,
message: `${a} / ${b} = ${c} (${d})`,
};
$resultLog = [result, ...$resultLog];
next();
}
function next() {
a = nextInt(1, max);
b = nextInt(1, a);
c = null;
d = null;
responseInput.focus();
}
function nextInt(min: number, max: number) {
return Math.floor(min + Math.random() * (max - min));
}
</script>
<div class="question">
<ChallengeValue value={a} />/<ChallengeValue value={b} />=<ChallengeInput
autofocus
bind:response={c}
bind:this={responseInput}
/>
<ChallengeInput bind:response={d} />
</div>
<style>
.question {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: repeat(2, 1fr);
align-content: center;
align-items: center;
}
</style>