{"id":"code-col-js-closure-var-settimeout","audience":"college","category":"coding","track":"coding","technique":"format","title":"Understand why a loop with setTimeout prints 3, 3, 3","free":false,"task":"Your web dev lab code prints 3 three times instead of 0, 1, 2. Ask for exactly 3 numbered lines labeled 'Prints:', 'Why:' and 'Question:', each under 25 words. 'Why:' must explain that var gives the whole function one shared i, and 'Question:' must ask something you can test by changing one keyword, without giving the answer. No code blocks, under 75 words.","materialLabel":"Your code and its output (attached to your prompt automatically)","material":"// lab6.js - WEB 230, Lab 6: timers and the event loop\n\nfunction scheduleMessages() {\n  for (var i = 0; i < 3; i++) {\n    setTimeout(function () {\n      console.log(\"message \" + i);\n    }, 100);\n  }\n  console.log(\"loop done\");\n}\n\nscheduleMessages();\n\n/* Output in the browser console:\n     loop done\n     message 3\n     message 3\n     message 3\n\n   What I expected:\n     message 0\n     message 1\n     message 2\n     (and maybe \"loop done\" first, that part makes sense)\n\n   Lab question 4 asks: \"Explain the output in your own words.\" So I\n   want to understand it, not copy an answer. TA said to look up\n   \"closures\" and \"block scope\" but the MDN page is long.\n*/","rules":[{"type":"numberedItems","count":3},{"type":"codeBlocks","count":0},{"type":"maxProseWords","max":75}],"mistakes":[{"title":"Getting the answer to the lab question","why":"The AI writes a full explanation you could paste into Lab question 4.","fix":"Say: \"Three short labeled lines; end with a question for me, not the answer.\"","checks":["r2","maxProseWords"]},{"title":"No labels","why":"Without labels, the output, the reason and the next step blur together.","fix":"Give them: \"Prints:, Why:, Question:\".","checks":["r0","numberedItems"]},{"title":"A vague why","why":"The AI says 'closures' without saying what's shared.","fix":"Ask: \"Why: what var does to i across the loop.\"","checks":["r1"]},{"title":"Code you didn't want","why":"The AI shows the let version in a code block.","fix":"Add: \"No code blocks or corrected code.\"","checks":["codeBlocks"]}],"promptHints":[{"label":"Gives the labels","patterns":["prints:","why:","question:"]},{"label":"Sets the number of lines","patterns":["\\b(3|three)\\b.{0,15}line","numbered"]},{"label":"Asks for a question back","patterns":["question","test (it )?myself","don'?t (give|tell)"]},{"label":"Rules out code","patterns":["no code","no corrected"]},{"label":"Caps each line","patterns":["\\d+\\s*words"]}],"hint":"If the lab asks for your own words, ask the AI for a nudge, not a paragraph. Fixed labels and a question to test keep the thinking yours.","rubricLabels":["Line 1 starts with 'Prints:' and gives the output: loop done, then message 3 three times","Line 2 starts with 'Why:' and says var makes one shared i for the whole function, which is 3 by the time the timers run","Line 3 starts with 'Question:', is a question ending in '?' that hints at changing var to let, and does not state the answer or show corrected code"]}