import {card,escape} from './common.js'; export async function attention(root){ root.innerHTML=card('Put your attention and memory to the test.',`
Name the ink color, even when the word says something different. A round has 16 shuffled trials: eight matching and eight clashing words.
Read the instructions, then start.
Responses are timed locally after the browser paints the stimulus. Correct-response medians are shown separately for matching and clashing words. A short round is an illustration, not a clinical attention test.
Watch the squares light up, then repeat the order. Each correct sequence adds one square. You control the flash speed and can replay a sequence.
Replaying provides extra practice. We mark that help in the result. This task illustrates working memory and rehearsal; it does not estimate IQ or diagnose a memory condition.
Clashing minus matching: ${Math.round(mb-ma)} ms. Your result may vary with practice, sample size, errors, and device timing.
`:''}`} function nextTrial(){if(!alive||mode!=='stroop')return;if(trial>=deck.length){current=null;$('#stroop-word').textContent='ROUND COMPLETE';$('#stroop-prompt').textContent='Compare your matching and clashing trials below.';$('#stroop-start').disabled=false;renderResults();return}current=deck[trial];$('#stroop-word').textContent=names[current.word];$('#stroop-word').style.color=colors[current.ink];$('#stroop-prompt').textContent=`Trial ${trial+1} of 16 · choose the INK color`;raf=requestAnimationFrame(()=>{raf=requestAnimationFrame(()=>{start=performance.now();accept=true;stroopControls(true)})})} function answer(color){if(!alive||!accept||mode!=='stroop')return;const ms=performance.now()-start;accept=false;stroopControls(false);const correct=color===current.ink;results.push({...current,correct,ms});trial++;$('#stroop-feedback').textContent=correct?'Correct. Get ready for the next word.':`The ink was ${names[current.ink].toLowerCase()}. Get ready for the next word.`;renderResults();$('#stroop-word').textContent='+';$('#stroop-word').style.color='#536f5d';timer=setTimeout(nextTrial,550)} $('#stroop-start').onclick=()=>{clearTimeout(timer);cancelAnimationFrame(raf);results=[];trial=0;deck=shuffle(Array.from({length:16},(_,i)=>{const ink=i%4,match=i<8;return {ink,word:match?ink:(ink+1+random(3))%4,match}}));$('#stroop-start').disabled=true;$('#stroop-feedback').textContent='Name the ink color. Use keys 1–4 or the buttons.';renderResults();nextTrial()}; $('#stroop-clear').onclick=()=>{clearTimeout(timer);cancelAnimationFrame(raf);accept=false;current=null;results=[];stroopControls(false);$('#stroop-start').disabled=false;$('#stroop-word').textContent='READY?';$('#stroop-word').style.color='#536f5d';renderResults();$('#stroop-feedback').textContent='Results cleared. Start when ready.'};root.querySelectorAll('[data-stroop]').forEach(b=>b.onclick=()=>answer(Number(b.dataset.stroop))); const keyboard=e=>{if(mode==='stroop'&&accept&&['1','2','3','4'].includes(e.key)){e.preventDefault();answer(Number(e.key)-1)}};document.addEventListener('keydown',keyboard); function pads(v){root.querySelectorAll('[data-memory]').forEach(b=>b.disabled=!v)} function clearPads(){root.querySelectorAll('[data-memory]').forEach(b=>b.classList.remove('lit'))} function showSequence(){const token=++seqToken;playing=true;entered=0;pads(false);clearPads();$('#memory-replay').disabled=true;$('#memory-feedback').textContent=`Watch ${sequence.length} flash${sequence.length===1?'':'es'}…`;const speed=Number($('#memory-speed').value);let i=0;function flash(){if(!alive||token!==seqToken||mode!=='memory')return;if(i>=sequence.length){playing=false;pads(true);$('#memory-replay').disabled=false;$('#memory-feedback').textContent=`Your turn: repeat ${sequence.length} square${sequence.length===1?'':'s'} in order.`;return}const b=root.querySelector(`[data-memory="${sequence[i]}"]`);b.classList.add('lit');timer=setTimeout(()=>{b.classList.remove('lit');i++;timer=setTimeout(flash,220)},speed)}timer=setTimeout(flash,450)} $('#memory-start').onclick=()=>{clearTimeout(timer);replayed=false;sequence=[random(4)];showSequence()};$('#memory-replay').onclick=()=>{clearTimeout(timer);replayed=true;showSequence()};$('#memory-speed').oninput=e=>$('#memory-speed-value').textContent=e.target.value+' ms'; root.querySelectorAll('[data-memory]').forEach(b=>b.onclick=()=>{if(playing||mode!=='memory')return;const value=Number(b.dataset.memory);if(value!==sequence[entered]){pads(false);$('#memory-feedback').textContent=`Sequence ended at length ${sequence.length}. Square ${sequence[entered]+1} was next. Replay it to practice or start again.`;$('#memory-replay').disabled=false;return}entered++;if(entered===sequence.length){completed++;longest=Math.max(longest,sequence.length);pads(false);$('#memory-results').textContent=`Longest sequence: ${longest} · Sequences completed: ${completed}${replayed?' · This sequence was replayed':''}`;$('#memory-feedback').textContent=`Correct! You repeated ${sequence.length}. One more square is coming.`;sequence.push(random(4));replayed=false;timer=setTimeout(showSequence,900)}else $('#memory-feedback').textContent=`${entered} of ${sequence.length} entered. Keep going.`}); function tab(value){clearTimeout(timer);cancelAnimationFrame(raf);accept=false;seqToken++;clearPads();pads(false);mode=value;$('#attention-stroop').hidden=value!=='stroop';$('#attention-memory').hidden=value!=='memory';$('#attention-stroop-tab').setAttribute('aria-selected',String(value==='stroop'));$('#attention-memory-tab').setAttribute('aria-selected',String(value==='memory'));stroopControls(false);$('#stroop-start').disabled=false;$('#memory-replay').disabled=!sequence.length} $('#attention-stroop-tab').onclick=()=>tab('stroop');$('#attention-memory-tab').onclick=()=>tab('memory');renderResults(); return()=>{alive=false;accept=false;seqToken++;clearTimeout(timer);cancelAnimationFrame(raf);document.removeEventListener('keydown',keyboard)}; }