Focus: Debugging & refactoring AI-generated code
Tools: ChatGPT, Claude, VS Code
You’ve been recruited by the Cyber Intelligence Bureau.
A rogue AI has been generating buggy, obscure, or chaotic JavaScript code across the web.
Your mission? Investigate these “mystery snippets,” figure out what they do, and clean them up.
You’ll be using your own dev skills and your trusted AI assistants (ChatGPT, Claude) to solve the case.
By completing this lab, you will:
✅ Practice analyzing unknown code snippets
✅ Learn to ask the right prompts for debugging and explanation
✅ Refactor code using best practices
✅ Critically assess and rewrite AI-generated code
✅ Add clear comments and documentation
- Fork this repo.
- Clone this repo.
- Upon completion, run the following commands:
git add .
git commit -m "Solved lab"
git push origin master- Create a Pull Request so that your work can be checked.
You’ll investigate 3 Mystery Snippets left behind by the rogue AI.
Each case contains confusing or inefficient code.
You must:
- Understand what the code does
- Rewrite it with better structure
- Comment your version
- Use AI as your assistant, not your crutch
Make sure the following are ready:
- ChatGPT Free
- Claude Free
- VS Code
- A local JS project folder (
/ai-detective-case-files) - Journal or Markdown notes to track your investigation
function mystery1(arr) {
let i = 0;
while (i < arr.length) {
if (arr[i] % 2 === 0) {
return true;
}
}
return false;
}- Ask AI what the code does and why it's broken.
- Fix the logic. Test it with sample arrays.
- Comment each step in plain English.
✅ Tip: Use Claude or ChatGPT:
"What’s wrong with this JS loop? Explain and fix it."
function q(q){return q.split('').reverse().join('')==q}- Decode what this function is really doing.
- Rename the function and variables for clarity.
- Add comments and reformat it to be readable.
✅ Tip: Ask the AI:
“Explain this function step-by-step and suggest better variable names.”
function complicatedCalc(a, b) {
let x = a;
let y = b;
for (let i = 0; i < b; i++) {
x = x + 1;
y = y - 1;
}
return x;
}- Figure out what this function really does.
- Refactor it to a one-liner (if possible).
- Compare your refactor with ChatGPT/Claude suggestions.
✅ Optional AI Prompt:
“Simplify this function in plain JavaScript. What’s the intent?”
For each case file:
- ✅
original.js→ The original snippet (as-is) - ✅
refactored.js→ Your rewritten version with comments - ✅
ai-summary.md→ Short reflection (AI used + what you learned)
Example format for ai-summary.md:
### Case 1: Infinite Loop
:brain: Prompt used: “Why is this loop never ending?”
:robot: AI suggested: Add `i++` in the loop.
:bulb: My Insight: It was checking even numbers but never moving forward.
:white_check_mark: Fix: Added `i++` and explained loop logic with comments.- What kind of AI feedback helped you most: debugging, naming, or rewriting?
- What did you learn about your own thinking by reviewing this code?
- Did you rely too much on AI in any step? How could you improve?
Find a weird code snippet online (Reddit, StackOverflow, GitHub Copilot, etc.)
Bring it into your /rogue-snippets/ folder, analyze it, and submit your fix!
🎁 Extra XP if you submit it in your pull request with a markdown write-up.
- Refactored
.jsfile with clear comments for each case -
ai-summary.mdwith prompt history & takeaways - (Optional) Bonus rogue snippet + your refactor
- Pull request submitted
Congratulations, detective.
You just cleaned up some of the chaos left behind by a generative ghost in the machine.
The codebase is safer because of you — and you’ve leveled up your AI literacy and debugging skills.
🧠 Remember: AI can generate 1000 lines in seconds…
But only you can make sense of them.
Happy coding!
