|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Math Quiz</title> |
| 5 | + <link |
| 6 | + rel="stylesheet" |
| 7 | + href="https://unpkg.com/mathlive/dist/mathlive-static.css" |
| 8 | + /> |
| 9 | + <style> |
| 10 | + math-field { |
| 11 | + width: 100%; |
| 12 | + border-radius: 8px; |
| 13 | + margin: 8px 0; |
| 14 | + } |
| 15 | + button { |
| 16 | + border-radius: 8px; |
| 17 | + padding: 8px; |
| 18 | + margin: 8px 0; |
| 19 | + font-size: 1em; |
| 20 | + font-weight: bold; |
| 21 | + font-family: system-ui; |
| 22 | + } |
| 23 | + p { |
| 24 | + font-family: system-ui; |
| 25 | + font-size: 1.5em; |
| 26 | + padding: 8px; |
| 27 | + } |
| 28 | + #feedback { |
| 29 | + font-family: system-ui; |
| 30 | + font-size: 1.2em; |
| 31 | + font-weight: bold; |
| 32 | + display: flex; |
| 33 | + justify-content: center; |
| 34 | + padding: 8px; |
| 35 | + border: 1px solid #ddd; |
| 36 | + border-radius: 8px; |
| 37 | + background: #f0f0f0; |
| 38 | + } |
| 39 | + </style> |
| 40 | +</head> |
| 41 | +<body> |
| 42 | + <p>Simplify the expression <span id="question"></span></p> |
| 43 | + <math-field id="answer"></math-field> |
| 44 | + <button id="submitAnswer">Check Answer</button> |
| 45 | + <div id="feedback"></div> |
| 46 | +</body> |
| 47 | +<script type="module"> |
| 48 | + import { renderMathInElement, convertLatexToMarkup } from "//unpkg.com/mathlive?module"; |
| 49 | + import "//unpkg.com/@cortex-js/compute-engine"; |
| 50 | + const ce = MathfieldElement.computeEngine; |
| 51 | + const question = generateRandomQuestion(); |
| 52 | + const expectedAnswer = question.simplify(); |
| 53 | + |
| 54 | + document.getElementById('question').innerHTML = |
| 55 | + convertLatexToMarkup(question.latex); |
| 56 | + |
| 57 | + const answerButton = document.getElementById('submitAnswer'); |
| 58 | + answerButton.addEventListener('click', checkAnswer); |
| 59 | + |
| 60 | + const answerField = document.getElementById('answer'); |
| 61 | + answerField.addEventListener('input', (event) => { |
| 62 | + if (event.data === 'insertLineBreak') checkAnswer(); |
| 63 | + }); |
| 64 | + |
| 65 | + function generateRandomQuestion() { |
| 66 | + const a = Math.floor(Math.random() * 10) + 1; |
| 67 | + const b = Math.floor(Math.random() * 10) ; |
| 68 | + const c = Math.floor(Math.random() * 10) + 1; |
| 69 | + const d = Math.floor(Math.random() * 10) ; |
| 70 | + // (ax+b)(cx+d) |
| 71 | + return ce.box(["Multiply", |
| 72 | + ["Add", ["Multiply", a, "x"], b], |
| 73 | + ["Add", ["Multiply", c, "x"], d]]); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + function checkAnswer() { |
| 78 | + const answer = document.getElementById('answer'); |
| 79 | + const studentInput = answer.expression; |
| 80 | + |
| 81 | + // Compare the expressions using `isSame()` |
| 82 | + const feedback = studentInput.isSame(expectedAnswer) ? |
| 83 | + 'Correct! 😃' : 'Try again. 😞'; |
| 84 | + |
| 85 | + document.getElementById('feedback').textContent = feedback; |
| 86 | + } |
| 87 | +</script> |
| 88 | +</html> |
0 commit comments