|
| 1 | +<html> |
| 2 | + <head> |
| 3 | + <title>Math Game</title> |
| 4 | + <link href=" https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel=" stylesheet" integrity=" sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin=" anonymous" > |
| 5 | + <script src=" https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity=" sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin=" anonymous" ></script> |
| 6 | + <style> |
| 7 | + body { |
| 8 | + --bs-body-color: #ecf0f1; |
| 9 | + --bs-body-bg: #7f8c8d; |
| 10 | + --bs-primary: #3498db; |
| 11 | + background-color: #2c3e50; |
| 12 | + height: 100vh; |
| 13 | + width: 100vw; |
| 14 | + } |
| 15 | + .invalid { |
| 16 | + background-color: #e74c3c !important; |
| 17 | + transition: all 0.2s ease-in-out; |
| 18 | + } |
| 19 | + </style> |
| 20 | + </head> |
| 21 | + <body> |
| 22 | + <div class="d-flex flex-column w-100 align-items-center h-100"> |
| 23 | + <h1 class="position-absolute">Math Game</h1> |
| 24 | + <div id="content" class="d-flex h-100 flex-column justify-content-center align-items-center"> |
| 25 | + <h2 id="equation">1 + 1</h2> |
| 26 | + <div class="d-flex flex-row gap-2"> |
| 27 | + <input id="answer" class="form-control" pattern="[0-9]{}" placeholder="Enter answer" type="text" onkeydown="checkEnter(this, event)"/> |
| 28 | + <button class="btn btn-primary" onclick="submitAnswer()">Submit</button> |
| 29 | + </div> |
| 30 | + </div> |
| 31 | + </div> |
| 32 | + <script> |
| 33 | + async function getEquation() { |
| 34 | + const resp = await fetch("/equation"); |
| 35 | + |
| 36 | + const equation = resp.ok ? await resp.text() : "1 + 1"; |
| 37 | + |
| 38 | + document.getElementById("equation").innerText = equation; |
| 39 | + document.getElementById("answer").innerText = ""; |
| 40 | + } |
| 41 | + |
| 42 | + async function submitAnswer() { |
| 43 | + const answerInput = document.getElementById("answer"); |
| 44 | + const answer = answerInput.value; |
| 45 | + |
| 46 | + answerInput.classList.remove("invalid"); |
| 47 | + try { |
| 48 | + const resp = await fetch("/answer", { |
| 49 | + method: "POST", |
| 50 | + body: answer, |
| 51 | + }); |
| 52 | + |
| 53 | + await getEquation() |
| 54 | + } catch { |
| 55 | + answerInput.classList.add("invalid"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + function checkEnter(element, e) { |
| 60 | + element.classList.remove('invalid') |
| 61 | + if (e.key === 'Enter') |
| 62 | + submitAnswer() |
| 63 | + } |
| 64 | + </script> |
| 65 | + </body> |
| 66 | +</html> |
0 commit comments