-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
40 lines (35 loc) · 1.07 KB
/
Copy pathutils.js
File metadata and controls
40 lines (35 loc) · 1.07 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
const operators = ['+', '-', '*', '/'];
export const generateQuestion = () => {
const op = operators[Math.floor(Math.random() * operators.length)];
let num1, num2, correct;
switch (op) {
case '+':
num1 = Math.floor(Math.random() * 10);
num2 = Math.floor(Math.random() * 10);
correct = num1 + num2;
break;
case '-':
num1 = Math.floor(Math.random() * 10);
num2 = Math.floor(Math.random() * num1); // ensures non-negative
correct = num1 - num2;
break;
case '*':
num1 = Math.floor(Math.random() * 10);
num2 = Math.floor(Math.random() * 10);
correct = num1 * num2;
break;
case '/':
num2 = Math.floor(Math.random() * 9) + 1;
correct = Math.floor(Math.random() * 10);
num1 = num2 * correct; // ensures whole number division
break;
}
const displayOp = op === '*' ? '×' : op === '/' ? '÷' : op;
const choices = [
correct,
correct + 1,
correct - 1,
correct + 2,
].sort(() => Math.random() - 0.5);
return { num1, num2, op, displayOp, correct, choices };
};