|
588 | 588 | const byDiff = {}; |
589 | 589 | for (const block of blocks) { |
590 | 590 | const lines = block.trim().split('\n').map(l => l.trim()).filter(Boolean); |
591 | | - const headerMatch = lines[0].match(/^(\d+)\s*,\s*(\d+)\s*:\s*(.+)$/); |
592 | | - const type = +headerMatch[1]; |
593 | | - const difficulty = +headerMatch[2]; |
594 | | - const question = headerMatch[3].trim(); |
| 591 | + const header = lines[0].match(/^(\d+)\s*,\s*(\d+)\s*:\s*(.+)$/); |
| 592 | + const type = +header[1]; |
| 593 | + const difficulty = +header[2]; |
| 594 | + const question = header[3].trim(); |
595 | 595 | const choices = {}; |
596 | 596 | let correct = null; |
597 | 597 | for (let i = 1; i < lines.length; i++) { |
598 | | - const match = lines[i].match(/^([a-cA-C])\s*:\s*(.+)$/); |
599 | | - const key = match[1].toUpperCase(); |
600 | | - choices[key] = match[2].trim(); |
601 | | - if (match[1] === match[1].toUpperCase()) correct = key; |
| 598 | + const m = lines[i].match(/^([a-cA-C])\s*:\s*(.+)$/); |
| 599 | + const key = m[1].toUpperCase(); |
| 600 | + choices[key] = m[2].trim(); |
| 601 | + if (m[1] === m[1].toUpperCase()) correct = key; |
602 | 602 | } |
603 | | - const timeLimit = 5000 + difficulty * 5000; |
604 | | - if (!byDiff[difficulty]) byDiff[difficulty] = []; |
605 | | - byDiff[difficulty].push({ type, difficulty, question, choices, correct, timeLimit }); |
| 603 | + byDiff[difficulty] ??= []; |
| 604 | + byDiff[difficulty].push({ type, difficulty, question, choices, correct, timeLimit: 5000 + difficulty * 5000 }); |
606 | 605 | } |
607 | 606 | const result = []; |
608 | 607 | const diffs = Object.keys(byDiff).map(Number).sort((a, b) => a - b); |
|
612 | 611 | const j = Math.floor(Math.random() * (i + 1)); |
613 | 612 | [problems[i], problems[j]] = [problems[j], problems[i]]; |
614 | 613 | } |
615 | | - problems = problems.slice(0, 5); |
616 | | - result.push(...problems); |
| 614 | + result.push(...problems.slice(0, 5)); |
617 | 615 | } |
618 | 616 | return result; |
619 | 617 | } |
|
1223 | 1221 | } |
1224 | 1222 | renderMathProblem(p); |
1225 | 1223 | startMathTimer(); |
1226 | | - // remove later |
1227 | | - console.log('difficulty:', p.difficulty) |
1228 | | - console.log('correct:', p.correct); |
1229 | 1224 | } |
1230 | 1225 | function renderMathProblem(p) { |
1231 | 1226 | if (p.type === 1) mathTypeEl.textContent = 'Simplify the polynomial'; |
|
0 commit comments