Word Scramble Game JavaScript Code: Preserve Every Letter

HTML for this example

Select the code and use your copy shortcut. Changes in this preview are temporary.

<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Word Scramble Game JavaScript Code: Preserve Every Letter</title><style>*{box-sizing:border-box}body{margin:0;color:var(--ink,#233342);background:var(--bg,#eef2f3);font:17px/1.6 system-ui,sans-serif}main{max-width:980px;margin:auto;padding:clamp(16px,4vw,38px)}h1{font:700 clamp(30px,5vw,49px)/1.12 Georgia,serif;margin:10px 0 20px;max-width:800px}h2{font-size:24px;line-height:1.3;margin:0 0 15px}h3{font-size:19px;margin:12px 0}p{margin:0 0 16px}.eyebrow{font-weight:750;letter-spacing:.08em;text-transform:uppercase;font-size:16px}header{padding:10px 0 25px;border-bottom:3px solid var(--accent,#24586d);margin-bottom:24px}.lede{font-size:20px;max-width:760px}.panel{background:var(--paper,#fff);padding:clamp(18px,3vw,28px);border:1px solid var(--line,#c6d1d8);border-radius:12px;margin:20px 0}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:20px}.grid>*{min-width:0}.row{display:flex;gap:12px;flex-wrap:wrap;align-items:center;margin:16px 0}.stack>*{margin:12px 0}button{font:700 17px/1.4 system-ui,sans-serif;cursor:pointer;color:white;background:var(--accent,#24586d);border:2px solid var(--accent,#24586d);border-radius:7px;padding:11px 17px;max-width:100%;overflow-wrap:anywhere}button.secondary{background:transparent;color:var(--ink,#233342)}button[aria-pressed=true]{box-shadow:inset 0 0 0 3px #fff;outline:2px solid var(--ink,#233342)}button:disabled{opacity:.55;cursor:default}input,textarea,select{font:17px/1.5 system-ui,sans-serif;color:inherit;background:white;border:1px solid #778591;padding:10px 12px;border-radius:6px;max-width:100%;width:100%;min-width:0}input[type=checkbox]{width:22px;height:22px}textarea{min-height:110px;resize:vertical}label{display:block;font-weight:700;margin:12px 0 7px}button:focus-visible,input:focus-visible,textarea:focus-visible,summary:focus-visible,[tabindex]:focus-visible{outline:3px solid #aa3d70;outline-offset:4px}.result{padding:18px;border-left:5px solid var(--accent,#24586d);background:var(--soft,#e5eff2);white-space:pre-wrap;overflow-wrap:anywhere;margin:20px 0}.result:empty{display:none}.hint{font-size:16px;color:var(--muted,#435968)}table{border-collapse:collapse;width:100%;table-layout:fixed;margin:16px 0}th,td{text-align:left;vertical-align:top;overflow-wrap:anywhere;padding:12px 8px;border-bottom:1px solid var(--line,#c6d1d8)}caption{text-align:left;font-weight:750;margin:8px 0}details{padding:18px;border:1px dashed #7b8b96;margin:20px 0}summary{cursor:pointer;font-weight:750}pre{font:16px/1.6 ui-monospace,monospace;white-space:pre-wrap;overflow-wrap:anywhere;background:#162735;color:#f5f8fa;padding:18px;border-radius:8px}canvas,svg{display:block;max-width:100%;height:auto}footer{margin:28px 0;font-size:16px}.badge{font-weight:750;color:var(--accent,#24586d)}.metric{font:700 38px/1.3 Georgia,serif}.board{display:grid;gap:5px;max-width:440px;margin:20px auto}.board button{padding:7px;min-height:45px}.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%)}@media(max-width:560px){.grid{grid-template-columns:1fr}main{padding:16px}th,td{padding:10px 5px}.row button{flex-grow:1}.metric{font-size:32px}}
:root{--bg:#f4eee0;--paper:#fffdf6;--accent:#9a5c29;--soft:#f8e7c9;--ink:#4b351f}.scramble{font:700 clamp(30px,7vw,48px)/1.3 ui-monospace,monospace;letter-spacing:.06em;overflow-wrap:anywhere}</style></head><body><main><header><p class="eyebrow">Letter lab · Two authored items</p><h1>Word Scramble Game JavaScript Code: Preserve Every Letter</h1><p class="lede">A wrong valid-letter answer resolves the card once. An invalid input does not.</p><p class="hint">Original fictional sample. Changes last only while this page remains open.</p></header><section class="panel"><h2 id="item">Ready · Two authored words</h2><p id="clue">Start to reveal the first clue.</p><p id="scramble" class="scramble"></p><label for="answer">Your answer, ASCII letters only</label><input id="answer" autocomplete="off"><div class="row"><button id="start" type="button">Start</button><button id="check" type="button">Check</button><button id="skip" type="button">Skip</button><button id="next" type="button">Next</button><button id="reset" type="button" class="secondary">Reset</button></div><p id="status" class="result" role="status">Ready · Score 0 of 2 · Resolved 0 of 2.</p><p id="result" role="status"></p><p id="reveal"></p><h2>Answer history</h2><pre id="history"></pre></section><footer>Compare BAL LOON with BALON: one leaves the card open, and the other ends it incorrectly.</footer></main><script>
(()=>{'use strict';const $=id=>document.getElementById(id);
const bank=[{id:'W1',answer:'BALLOON',clue:'A party object filled with air',scramble:'LOBOLAN'},{id:'W2',answer:'PLANET',clue:'A world orbiting a star',scramble:'TENLAP'}];let phase='Ready',index=0,score=0,history=[];const multiset=s=>s.split('').sort().join('');function validBank(){return new Set(bank.map(x=>x.id)).size===2&&bank.every(x=>x.id&&x.clue.trim()&&/^[A-Z]+$/.test(x.answer)&&multiset(x.answer)===multiset(x.scramble));}function status(){$('check').disabled=phase!=='Playing';$('skip').disabled=phase!=='Playing';$('next').disabled=phase!=='Resolved'||index!==0;$('start').disabled=phase!=='Ready';$('answer').disabled=phase!=='Playing';$('status').textContent=phase+' · Score '+score+' of 2 · Resolved '+history.length+' of 2.';$('history').textContent=history.join('\n');}function show(){const x=bank[index];$('item').textContent=x.id;$('clue').textContent=x.clue;$('scramble').textContent=x.scramble;$('answer').value='';$('result').textContent='';$('reveal').textContent='';status();}function resolve(skip=false){if(phase!=='Playing')return;const raw=$('answer').value.trim();if(!skip&&!/^[A-Za-z]+$/.test(raw)){$('result').textContent='Enter nonempty ASCII letters only; internal spaces are invalid. Item remains unresolved.';return;}const x=bank[index],normalized=skip?'[explicit skip]':raw.toUpperCase(),correct=!skip&&normalized===x.answer,outcome=skip?'Skipped':correct?'Correct':'Incorrect';if(correct)score++;history.push(x.id+' · '+normalized+' · '+outcome+' · Credit '+(correct?1:0)+' · Clue: '+x.clue+' · Scramble: '+x.scramble);phase=index===1?'Complete':'Resolved';$('result').textContent=outcome+'. This item is resolved once.';$('reveal').textContent='Authored answer: '+x.answer;status();}$('start').addEventListener('click',()=>{if(phase!=='Ready')return;if(!validBank()){$('result').textContent='Configured bank invalid; cannot start.';return;}phase='Playing';show();});$('check').addEventListener('click',()=>resolve());$('skip').addEventListener('click',()=>resolve(true));$('next').addEventListener('click',()=>{if(phase!=='Resolved'||index!==0)return;index=1;phase='Playing';show();$('answer').focus();});$('answer').addEventListener('keydown',event=>{if(event.key==='Enter'&&!event.repeat){event.preventDefault();resolve();}});$('reset').addEventListener('click',()=>{phase='Ready';index=0;score=0;history=[];$('item').textContent='Ready · Two authored words';$('clue').textContent='Start to reveal the first clue.';$('scramble').textContent='';$('answer').value='';$('result').textContent='';$('reveal').textContent='';status();});status();
})();
</script></body></html>