Saving the Full Experiment Configuration
A practical guide to building a reliable web-based N-back experiment: how to log experiment configuration, run data integrity checks before export, validate results against an acceptance checklist, verify scoring with a manual test case, and troubleshoot common implementation errors.
Save the complete experiment configuration alongside the results, including stimulus duration, response window, the number of blocks, the screen refresh rate (if the browser can provide it), the browser version, and the page version number. When anomalies occur, this information helps researchers pinpoint which stage of the experiment went wrong.
Data Integrity Checks
Run the following checks before exporting data. If any check fails, notify the user and prevent the data from being saved as valid results.
- Trial count: The length of
trialsmust equal T. - Contiguous indices:
trialIndexmust appear consecutively from 0 to T−1, with no gaps or duplicates. - Valid responses:
responsemust be one of exactly three values: “Yes”, “No”, or “Missed”. - Reaction time consistency: When
responseis “Missed”,reactionTimemust be empty; in all other cases it must be a non-negative number. - Monotonic timing:
onsetTimemust be strictly increasing, and the difference between adjacent trials must not deviate from 1000 ms by more than a pre-defined tolerance. The tolerance should be determined before the experiment begins and written into the configuration. - Delay logging: If any frame is found to have occurred after the scheduled trigger time, record the delay amount and flag that trial. Never silently skip it.
Acceptance Checklist
The following five items are the acceptance criteria for this page. Each includes a verification method for practical checking.
- N value locked: Once the experiment starts, the N selection control is disabled. Verification: after starting, attempt to change it — the interface should not respond, and the N field in the exported data should remain consistent.
- Fixed stimulus set: Every presented character must belong to the specified 10 Chinese characters. Verification: deduplicate the exported
stimulusvalues — the result should exactly equal that set. - One-second triggering: The difference between adjacent
onsetTimevalues must fall within the tolerance. Verification: compute the distribution of differences after export. - Complete response recording: Every trial must have “Yes”, “No”, or “Missed”. Verification: the sum of the three categories must equal T.
- Correct output: Accuracy, hit rate, and false alarm rate are computed using the formulas described earlier; when a denominator is zero, the value should be empty. Verification: see the manual test case below.
Manual Test Case
Using the sequence “国 火 国 道 火 道” with N=2, verify that the scoring is correct. The first two trials (indices 0 and 1) are excluded from scoring, so the scored trials are indices 2 through 5, giving S=4 scored trials.
- Index 2 (国, target): response “Yes” — a hit, correct.
- Index 3 (道, non-target): response “No” — a correct rejection, correct.
- Index 4 (火, non-target): response “Yes” — a false alarm, incorrect.
- Index 5 (道, target): response “Missed” — a miss, incorrect.
There are Nt=2 target trials and Nn=2 non-target trials. This yields:
- Accuracy = 2 / 4 = 0.5
- Hit rate = 1 / 2 = 0.5
- False alarm rate = 1 / 2 = 0.5
- Number of missed responses = 1
If the code output does not match these values, first check whether indices 0 and 1 were included in the statistics, or whether “Missed” was treated as “No”.
Troubleshooting Common Errors
- First N trials counted in the statistics: Accuracy is systematically lowered. Check whether scoring starts from trial N+1 (index N).
- Missed responses recorded as “No”: The false alarm rate and accuracy will not match manual verification. Check that the default value of
responseis “Missed”, and that “No” comes only from a keypress or button. - Zero output when the denominator is zero: For example, if a sequence segment contains no target trials, the hit rate shows as 0. In this case, output an empty value and display “Not computable” in the interface.
- Chained setTimeout scheduling: The longer the experiment runs, the larger the drift in later trials. Instead, compute each trial from absolute times based on t0.
- Not filtering key repeats: A single long keypress generates multiple “Yes” responses, inflating the false alarm rate. Only handle key events where
event.repeatis false. - Data continuity after tab switching: Trials with abnormal timing appear after the page is restored. Check whether
visibilitychangeproduced a pause record, and confirm that the affected trials have been flagged. - IME intercepting keystrokes: Some participants’ keypresses go unregistered. Check whether events with
isComposingset to true are being ignored, instruct participants to switch to the English input mode, and keep buttons available as a fallback input method.
