Spaces:
Running
Running
Download app.js from alphabell/Kernel-Trap-Cards: direct link, hf CLI and curl.
- Browser
- Download file 3.86 kB
-
https://huggingface.co/spaces/alphabell/Kernel-Trap-Cards/resolve/main/app.js
- Command line
-
hf download hf://spaces/alphabell/Kernel-Trap-Cards/app.js
-
curl -L -o app.js https://huggingface.co/spaces/alphabell/Kernel-Trap-Cards/resolve/main/app.js
3.86 kB
| import { cases, runTest } from './cases.mjs'; | |
| const byId = (id) => document.getElementById(id); | |
| const initialSlug = new URLSearchParams(location.search).get('case'); | |
| let current = Math.max(0, cases.findIndex((item) => item.slug === initialSlug)); | |
| let score = 0; | |
| function displayValue(value) { | |
| if (value === null) return 'unwritten'; | |
| if (value === Infinity) return 'Infinity'; | |
| if (value === -Infinity) return '-Infinity'; | |
| return String(value); | |
| } | |
| function renderCase() { | |
| const item = cases[current]; | |
| byId('case-card').hidden = false; | |
| byId('final-card').hidden = true; | |
| byId('progress').textContent = `Case ${current + 1} of ${cases.length}`; | |
| byId('score').textContent = `Caught: ${score}`; | |
| byId('case-number').textContent = `${String(current + 1).padStart(2, '0')} / ${String(cases.length).padStart(2, '0')}`; | |
| byId('case-title').textContent = item.title; | |
| byId('case-task').textContent = item.task; | |
| byId('fixed-test').textContent = item.fixed; | |
| byId('code').textContent = item.code; | |
| byId('question').textContent = item.question; | |
| byId('case-link').href = `?case=${encodeURIComponent(item.slug)}`; | |
| byId('feedback').hidden = true; | |
| byId('feedback').textContent = ''; | |
| byId('next').hidden = true; | |
| byId('choices').replaceChildren(); | |
| history.replaceState({}, '', `?case=${encodeURIComponent(item.slug)}`); | |
| item.choices.forEach((choice, index) => { | |
| const button = document.createElement('button'); | |
| button.type = 'button'; | |
| button.className = 'choice'; | |
| button.innerHTML = `<span class="choice-index">${String.fromCharCode(65 + index)}</span><span></span><span class="choice-arrow" aria-hidden="true">→</span>`; | |
| button.children[1].textContent = choice.label; | |
| button.addEventListener('click', () => choose(item, choice, index)); | |
| byId('choices').append(button); | |
| }); | |
| } | |
| function choose(item, choice, index) { | |
| if (!byId('next').hidden) return; | |
| const mismatch = runTest(item, choice); | |
| if (mismatch) score++; | |
| const correctIndex = item.choices.findIndex((candidate) => runTest(item, candidate) !== null); | |
| [...byId('choices').children].forEach((button, i) => { | |
| button.disabled = true; | |
| if (i === index) button.classList.add('selected'); | |
| if (i === correctIndex) button.classList.add('correct'); | |
| }); | |
| const feedback = byId('feedback'); | |
| const detail = mismatch || runTest(item, item.choices[correctIndex]); | |
| const lead = mismatch ? 'Caught it.' : 'Still green.'; | |
| const evidence = `At index ${detail.index}, the reference returns ${displayValue(detail.expected)}; the candidate returns ${displayValue(detail.observed)}.`; | |
| feedback.className = `feedback ${mismatch ? 'caught' : 'missed'}`; | |
| feedback.innerHTML = '<strong></strong><p></p><p></p>'; | |
| feedback.children[0].textContent = lead; | |
| feedback.children[1].textContent = mismatch | |
| ? evidence | |
| : `Your chosen test matches the reference. Try ${item.choices[correctIndex].label.toLowerCase()}: ${evidence}`; | |
| feedback.children[2].textContent = item.lesson; | |
| feedback.hidden = false; | |
| byId('score').textContent = `Caught: ${score}`; | |
| byId('next').textContent = current === cases.length - 1 ? 'See result' : 'Next trap'; | |
| byId('next').hidden = false; | |
| byId('next').focus(); | |
| } | |
| function renderEnd() { | |
| byId('case-card').hidden = true; | |
| byId('final-card').hidden = false; | |
| byId('progress').textContent = 'Complete'; | |
| byId('score').textContent = `Caught: ${score}`; | |
| byId('case-link').href = '?case=tail-mask'; | |
| byId('final-title').textContent = `You caught ${score} of ${cases.length}.`; | |
| byId('restart').focus(); | |
| } | |
| byId('next').addEventListener('click', () => { | |
| if (current < cases.length - 1) { | |
| current++; | |
| renderCase(); | |
| byId('case-title').focus?.(); | |
| } else renderEnd(); | |
| }); | |
| byId('restart').addEventListener('click', () => { | |
| current = 0; | |
| score = 0; | |
| renderCase(); | |
| }); | |
| renderCase(); | |