-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_practice.js
More file actions
114 lines (97 loc) · 2.78 KB
/
math_practice.js
File metadata and controls
114 lines (97 loc) · 2.78 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
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
const { ALL } = require('dns');
const readline = require('readline');
/**
* queue: list of math problems to solve
*/
let MAX_QUEUE_SIZE = 10
let QUEUE = Array(MAX_QUEUE_SIZE).fill(null)
let current_round = 0
const STD_OPERATIONS = {
'*' : (a, b) => a * b,
'/' : (a, b) => a / b,
'+' : (a, b) => a + b,
'-' : (a, b) => a - b,
'%' : (a, b) => a % b,
'^' : (a, b) => a ** b
}
// Create readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Set stdin in raw mode to read characters individually
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
function exit() {
process.exit()
}
function print(str) {
process.stdout.write(str)
}
function clear_page() {
console.clear()
}
function clear_line() {
print('\x1b[2K\r ');
}
function clear_below_line(lineNumber) {
// Move the cursor to the beginning of the desired line
process.stdout.write(`\x1b[${lineNumber};1H`);
// Clear the screen from the cursor down
process.stdout.write('\x1b[J ');
}
function generate_problem_set() {
current_round++
const operations = Object.keys(STD_OPERATIONS)
const problem_set = []
for (let i = 0; i < MAX_QUEUE_SIZE; i++) {
const a = Math.floor(Math.random() * 10)
const b = Math.floor(Math.random() * 10)
const operation = operations[Math.floor(Math.random() * operations.length)]
problem_set.push({a, b, operation})
}
QUEUE = problem_set
}
function print_header() {
print(`\n Typing Practice - round ${current_round}\n`)
}
const question = (query) => new Promise((resolve) => rl.question(query, resolve));
async function prompt_question(problem=null) {
if (QUEUE.length === 0) {
generate_problem_set()
}
if (problem === null) {
problem = QUEUE.shift()
}
console.log(problem)
print_header()
const problem_string = `\n ${problem.a} ${problem.operation} ${problem.b} = `
const answer = STD_OPERATIONS[problem.operation](problem.a, problem.b).toFixed(2)
// get user input
const userInput = await question(problem_string)
const is_correct = parseFloat(userInput) == answer
return {
userInput,
is_correct,
problem,
answer,
}
}
async function main() {
generate_problem_set()
clear_page()
let response = await prompt_question()
while (true) {
while (!response.is_correct && !response.skip) {
console.log(`Incorrect! Try again.\n`)
console.log(`answer ${response.answer}`)
console.log(`input ${response.userInput}`)
response = await prompt_question(response.problem)
}
clear_page()
response = await prompt_question()
}
exit()
}
main()