Socratic Debugging of Loops and Conditionals
In this lab, you will diagnose and fix three broken JavaScript programs, but you will not receive a single line of corrected code. Instead, you will work with Claude in the AI as Socratic Tutor role: the AI asks guiding questions, you trace the code by hand, and you write every fix yourself. Debugging logic that someone else wrote is one of the most common tasks in professional software development, and the ability to reason through a program line by line is a skill that interviewers and teammates will expect from you.
Time: 45 minutes (asynchronous)
Scenario
A developer on your team has just rotated to another project, and your supervisor has handed you three of their small utility scripts: a shopping cart total calculator, a temperature range checker, and a feedback function for the number guessing game your team prototyped. All three scripts contain logical errors. None of them produce the output the team expects, and one of them does not stop running at all.
Your supervisor has paired you with a senior engineer who mentors juniors under a strict policy: the mentor never touches your keyboard, never points at the broken line, and never tells you the answer. The mentor only asks questions. In this lab, Claude plays that mentor. Your job is to find each root cause yourself, explain it in your own words, and repair the code.
Learning Objectives
By the end of this activity, you will have demonstrated your ability to:
- Diagnose and fix common logical errors in loops and conditionals, including off-by-one errors, infinite loops, misplaced statements, and unreachable branches.
- Trace variable values by hand to predict and verify a program’s behavior.
- Direct an AI assistant into the Socratic Tutor role and keep it in that role for an entire debugging session.
- Document root causes and debugging evidence in a Git repository.
The AI Role in This Lab: Socratic Tutor
This course teaches three distinct ways of working with AI, and you will practice each one by name: AI as Socratic Tutor, AI as Architect, and AI as Builder. This lab uses the first role. As a Socratic Tutor, the AI is explicitly forbidden from writing code or giving direct answers. It may only ask guiding questions, request that you trace values by hand, and ask you to predict output.
This constraint is the point of the lab, not an obstacle. When an AI hands you a finished fix, you learn what the fix looks like. When an AI asks you the right question, you learn how to find the fix, and that skill transfers to every bug you will ever face. Answer every question honestly, even when it feels slow. The value of this lab lives in the tracing you do by hand.
Part 1: Set Up Your Project
Time: about 5 minutes
-
Open a terminal, then create and initialize the project:
mkdir socratic-debugging-lab cd socratic-debugging-lab git init -
Create three files named
bug-1.js,bug-2.js, andbug-3.js, and copy the starter code below into them exactly as written. Do not fix anything while copying, even if you already spot a problem. Each file’s comment block states what the program is supposed to do and the exact expected output.
bug-1.js: Shopping Cart Total
// bug-1.js
// Adds up the prices in a shopping cart. If the final total is greater
// than 50, a 10 percent discount should be applied one time, to the
// final total only.
//
// Expected output:
// Cart total: $54.00
let prices = [12, 40, 8];
function calculateTotal(priceList) {
let total = 0;
for (let i = 0; i <= priceList.length; i++) {
total = total + priceList[i];
if (total > 50) {
total = total * 0.9;
}
}
return total;
}
console.log("Cart total: $" + calculateTotal(prices).toFixed(2));bug-2.js: Temperature Range Checker
// bug-2.js
// Counts how many temperature readings are outside the safe range.
// A reading is out of range when it is below 65 or above 85.
//
// Expected output:
// Checked 7 readings.
// 2 readings were out of range.
let readings = [68, 72, 90, 75, 60, 79, 83];
let i = 0;
let outOfRange = 0;
while (i < readings.length) {
if (readings[i] < 65 && readings[i] > 85) {
outOfRange = outOfRange + 1;
i = i + 1;
}
}
console.log("Checked " + i + " readings.");
console.log(outOfRange + " readings were out of range.");bug-3.js: Guessing Game Feedback
// bug-3.js
// Gives a player feedback on a guess in the number guessing game from
// Lesson 4. Math.abs() returns the distance between two numbers as a
// positive value.
//
// Feedback rules:
// - Distance 0 (exactly right): "Correct! You found the secret number."
// - Distance 1 or 2: "Very close!"
// - Distance 3, 4, or 5: "Getting warm."
// - Distance 6 or more: "Cold guess. Try again."
//
// Expected output:
// Very close!
// Getting warm.
// Cold guess. Try again.
// Correct! You found the secret number.
let game = {
secretNumber: 42,
};
function getFeedback(guess) {
let distance = Math.abs(guess - game.secretNumber);
if (distance === 0) {
return "Correct! You found the secret number.";
} else if (distance <= 5) {
return "Getting warm.";
} else if (distance <= 2) {
return "Very close!";
} else {
return "Cold guess. Try again.";
}
}
console.log(getFeedback(41));
console.log(getFeedback(45));
console.log(getFeedback(60));
console.log(getFeedback(42));-
Run each program with Node and write down exactly what you observe. You will report these observations to your tutor in Part 3.
node bug-1.js node bug-2.js node bug-3.js
One of these programs contains an infinite loop, a failure mode you studied in Lesson 3. When a program in the terminal never finishes, press Ctrl+C to stop it. If you run these files in a browser console instead of Node, an infinite loop can freeze the entire tab, so Node is the recommended way to run this lab.
-
Commit the unmodified starter files so that your later fixes show up as clean diffs:
git add bug-1.js bug-2.js bug-3.js git commit -m "Add buggy starter files"
If you do not have access to a Pro-level Claude account through Per Scholas, you may complete this lab using the free tier of Claude or another AI assistant with equivalent capabilities. Free-tier usage limits may require you to complete the lab across more than one session. If you are working on a free tier, keep each bug in its own short conversation to stay within message limits.
Part 2: Set Up Your Socratic Tutor
Time: about 5 minutes
The following prompt places Claude in the Socratic Tutor role and keeps it there. Copy it exactly.
You are my Socratic debugging tutor for a JavaScript lab. I am a learner
reviewing loops and conditionals, and I must find and fix every bug myself.
Follow these rules for the entire conversation, with no exceptions:
1. Never write, complete, or correct code for me. Do not show fixed code,
partial fixes, or corrected lines, even if I ask for them.
2. Never name the faulty line or state the fix directly.
3. Respond only with guiding questions, requests for me to trace variable
values by hand, and requests for me to predict what the code will output.
4. Ask one question at a time and wait for my answer before continuing.
5. If I ask you for the answer, politely decline and redirect me with a
narrower question about the code.
6. When I state the root cause of a bug correctly in my own words, confirm
my understanding by asking one final verification question, then ask me
what change I plan to make. Do not evaluate my planned change beyond
asking questions.
I will paste one program at a time, describe what I expected it to do, and
describe what it actually did.Choose one of the two setups below. Option A is the default for this lab; Option B produces the same dialogue inside your terminal.
Option A: claude.ai (recommended)
- Sign in at https://claude.ai with your Per Scholas-provided Claude Pro account.
- Create a new Project named
Socratic Debugging Tutorand paste the prompt above into the Project’s custom instructions. Custom instructions apply to every conversation inside the Project, which makes the role enforcement reliable across all three bugs. - Start one new conversation inside the Project for each bug. Starting fresh per bug keeps each dialogue short, focused, and easy to share later.
If you are using the free tier of Claude, Projects may not be available. In that case, paste the prompt as the very first message of each new conversation instead, before you paste any code.
Option B: Claude Code in Plan Mode
-
If Claude Code is not installed yet, install it, then start it from inside the
socratic-debugging-labfolder you created in Part 1:npm install -g @anthropic-ai/claude-code claudeIf your terminal is no longer inside the project folder, move back into it with
cdbefore runningclaude. Log in with your Per Scholas-provided Claude account when prompted (/login). -
Press Shift+Tab to cycle permission modes until you reach Plan Mode. In Plan Mode, Claude Code proposes and discusses without editing files, which matches the Socratic Tutor constraint.
-
Paste the Socratic Tutor prompt above as your first message.
-
Instead of pasting code, you can ask Claude Code to read a file directly, for example:
Read bug-1.js. I am debugging it. Here is what I expected and what happened...Claude Code runs inside your project folder and can read the file itself. -
You must not allow Claude Code to modify any file in this lab. If it ever asks permission to edit a file, deny the request. You type every fix yourself, in your own editor, because the Socratic Tutor role forbids AI-written code.
Whichever option you choose, the rule is the same: if the AI slips out of its role and offers you code or names the fix, do not use it. Reply that it has broken the rules, ask it to rephrase as a question, and continue. If it has already revealed a complete fix, start a fresh conversation for that bug so your submission still demonstrates the Socratic process.
Part 3: Debug the Three Programs
Time: about 25 minutes (roughly 8 minutes per bug)
Work through the bugs in order: bug-1.js, then bug-2.js, then bug-3.js. For each program, follow this protocol.
-
Open the dialogue. Start a new conversation with your tutor and state the expected behavior, the actual behavior, and the code. For example:
I am debugging bug-1.js. I expected it to print "Cart total: $54.00". Instead, here is what I observed: [describe exactly what you saw]. Here is the code: [paste the full contents of bug-1.js] -
Answer every question honestly. Your tutor will ask you to predict output and to trace variable values by hand. Tracing means walking through the loop one pass at a time and writing down what each variable holds. Here is what a hand trace looks like, using a shortened version of the countdown loop from Lesson 3:
let count = 3; while (count > 0) { console.log(count); count--; }Pass countwhen the condition is checkedcount > 0What happens 1 3 true prints 3, countbecomes 22 2 true prints 2, countbecomes 13 1 true prints 1, countbecomes 04 0 false loop ends Build a table like this whenever your tutor asks you to trace. It is slower than guessing, and that is exactly why it works.
-
State the root cause in your own words. When you believe you understand the bug, tell your tutor plainly what is wrong and why it produces the behavior you observed. A correct root cause explains the behavior, not just the location. Your tutor will confirm your understanding with a final question rather than a direct answer.
-
Write the fix yourself. Edit the file in your own editor. Do not ask the AI for the fix, and do not accept one if it is offered. A program may contain more than one problem, so you are finished with a file only when its output matches the expected output in its comment block exactly.
-
Verify. Rerun the program and compare against the expected output:
node bug-1.js -
Checkpoint: record and commit. Add an entry for the bug to a file named
NOTES.md(template below), review your change withgit diff, then commit with a message that names the bug:git diff git add bug-1.js NOTES.md git commit -m "Fix bug 1: describe the root cause in a few words"Reviewing your own diff before every commit is a habit you will rely on heavily later in this course, when the AI moves into the Builder role and you become the reviewer.
-
Repeat the protocol for
bug-2.jsandbug-3.js, each in its own conversation and its own commit.
Use this template for NOTES.md:
# Debugging Notes
## Bug 1: Shopping Cart Total
- **Observed behavior:**
- **Root cause, in my own words (one or two sentences):**
- **The tutor question that helped me most:**
## Bug 2: Temperature Range Checker
- **Observed behavior:**
- **Root cause, in my own words (one or two sentences):**
- **The tutor question that helped me most:**
## Bug 3: Guessing Game Feedback
- **Observed behavior:**
- **Root cause, in my own words (one or two sentences):**
- **The tutor question that helped me most:**
## Reflection
1.
2.Part 4: Create Your Dialogue Record and Publish Your Repository
Time: about 5 to 10 minutes
-
Create a file named
DIALOGUE.mdcontaining evidence of your Socratic dialogues, one section per bug. Provide either of the following:- Shared conversation links. In claude.ai, use the Share button on each conversation to create a public link, and paste one link per bug into
DIALOGUE.md. - Transcript excerpts. If sharing is disabled in your workspace, or if you used Claude Code in the terminal, copy excerpts of each dialogue into
DIALOGUE.mdinstead. Each excerpt must show the tutor asking guiding questions, must show your own root-cause statement, and must not contain any AI-written code.
- Shared conversation links. In claude.ai, use the Share button on each conversation to create a public link, and paste one link per bug into
-
Complete the Reflection section of
NOTES.mdby answering the two questions in the Reflection section below. -
Commit the remaining files, create a new repository on GitHub named
socratic-debugging-lab, and push:git add NOTES.md DIALOGUE.md git commit -m "Add dialogue record and reflection" git remote add origin https://github.com/YOUR-USERNAME/socratic-debugging-lab.git git branch -M main git push -u origin main
Reflection
Answer both questions in the Reflection section of NOTES.md:
- How did answering your tutor’s questions differ from being handed the corrected code? What did the questions force you to understand that a direct answer would not have?
- Which hand-tracing technique from this lab will you reuse the next time you debug code on your own, and why?
Submission Guidelines
Submit the link to your GitHub repository via Canvas. Your repository must contain:
bug-1.js,bug-2.js, andbug-3.js, fixed so that each program produces the expected output shown in its comment block.NOTES.md, with all three bug entries completed and both reflection questions answered.DIALOGUE.md, with shared conversation links or transcript excerpts for all three bugs.- A commit history containing the starter-file commit plus at least three fix commits, one per bug.
Grading
This lab is graded complete/incomplete. To receive a grade of complete, all four of the following must be true:
- All three programs run and produce their expected output exactly.
DIALOGUE.mddemonstrates that the AI asked guiding questions and never supplied corrected code.NOTES.mdstates the root cause of each bug in your own words and names the single tutor question that helped you most, and both reflection answers are present.- The commit history contains the starter-file commit plus at least three fix commits, one per bug.
If any dialogue shows the AI writing or correcting code for you, that portion of the lab is incomplete. Restart that bug in a fresh conversation with the Socratic Tutor prompt and resubmit. The graded skill in this lab is your reasoning, not the finished files.