Library › Coding AI › College students
Understand why a loop with setTimeout prints 3, 3, 3
Practice: Asking for a format you can use. About 5 minutes.
About this exercise
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.
Common mistakes when you ask AI for this
- Getting the answer to the lab question. The AI writes a full explanation you could paste into Lab question 4. Try: Say: "Three short labeled lines; end with a question for me, not the answer."
- No labels. Without labels, the output, the reason and the next step blur together. Try: Give them: "Prints:, Why:, Question:".
- A vague why. The AI says 'closures' without saying what's shared. Try: Ask: "Why: what var does to i across the loop."
- Code you didn't want. The AI shows the let version in a code block. Try: Add: "No code blocks or corrected code."
What a strong prompt includes
- Gives the labels
- Sets the number of lines
- Asks for a question back
- Rules out code
- Caps each line
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)