State Tracing
Track variables through programs. Predict output before running.
The skill: Computational empathy. Think like the machine thinks.
Time: 5-6 hours total
This week uses: Variables · Assignment (=) · console.log() · Strings · Numbers · Arithmetic (+, -, *, /) · String concat (+) · if/else · Comparison (==, ===, <, >) · for loop · while loop · Arrays [] · Modulo (%)
Covered in Lesson 1. If any feel unfamiliar, review before continuing.
Estimated time remaining: 5-6 hours
The Format
- 1. Read the code
- 2. Trace the state (fill in the table)
- 3. Predict the output
- 4. Reveal the answer
- 5. Reflect — where did your mental model break?
The state table is the artifact. It externalizes the mind's model of execution.
Exercise 1.1: Variable Assignment
Variables hold values. Assignment copies the right side into the left. Values can change.
Simple Assignment
1let a = 5;2let b = 10;3let c = a;4console.log(a, b, c);| Step | a | b | c |
|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Reassignment
1let x = 1;2x = 2;3x = 3;4console.log(x);| Step | x |
|---|---|
What will this code print? Commit to your answer before revealing.
Copying vs. Linking
1let first = 100;2let second = first;3first = 200;4console.log(first, second);| Step | first | second |
|---|---|---|
What will this code print? Commit to your answer before revealing.
Exercise 1.2: Arithmetic and String Operations
Expressions evaluate right-to-left. Operators have precedence. `+` with strings concatenates.
Basic Arithmetic
1let a = 10;2let b = 3;3let sum = a + b;4let diff = a - b;5let product = a * b;6console.log(sum, diff, product);| Step | a | b | sum | diff | product |
|---|---|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Order of Operations
1let result = 2 + 3 * 4;2let other = (2 + 3) * 4;3console.log(result, other);| Step | result | other |
|---|---|---|
What will this code print? Commit to your answer before revealing.
String Concatenation
1let greeting = "Hello";2let name = "World";3let message = greeting + " " + name;4console.log(message);| Step | greeting | name | message |
|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Mixed Types (Bonus Challenge)
1let x = "3";2let y = 2;3let a = x + y;4let b = y + y;5console.log(a, b);| Step | x | y | a | b |
|---|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Exercise 1.3: Conditional Branching
Code doesn't always run top-to-bottom. Conditions choose paths. Only one path executes.
Simple If/Else
1let temperature = 75;2let message;3 4if (temperature > 80) {5 message = "Hot";6} else {7 message = "Not hot";8}9 10console.log(message);| Step | temperature | message | Branch taken |
|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Chained Conditions
1let score = 85;2let grade;3 4if (score >= 90) {5 grade = "A";6} else if (score >= 80) {7 grade = "B";8} else if (score >= 70) {9 grade = "C";10} else {11 grade = "F";12}13 14console.log(grade);| Step | score | grade | Branch taken |
|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Truthiness
1let username = "";2let status;3 4if (username) {5 status = "Logged in";6} else {7 status = "Guest";8}9 10console.log(status);| Step | username | status | Branch taken |
|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Epistemic humility moment: JavaScript's truthiness rules are notoriously confusing. Even experienced programmers look them up. Know this is a place where your intuition might fail.
Exercise 1.4: Loop Iteration
Loops repeat. The iteration variable changes. The loop stops when the condition fails.
Counting Loop
1for (let i = 0; i < 3; i++) {2 console.log(i);3}| Iteration | i (start) | Condition | i (after ++) |
|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Accumulator Pattern
1let sum = 0;2 3for (let i = 1; i <= 4; i++) {4 sum = sum + i;5}6 7console.log(sum);| Iteration | i | sum (before) | sum (after) |
|---|---|---|---|
What will this code print? Commit to your answer before revealing.
While Loop
1let count = 5;2 3while (count > 0) {4 console.log(count);5 count = count - 1;6}| Iteration | count (start) | Output | count (end) |
|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Exercise 1.5: Mixed Tracing
Combine all concepts. Real programs mix variables, arithmetic, conditions, and loops.
Loop with Conditional
1for (let i = 1; i <= 5; i++) {2 if (i % 2 === 0) {3 console.log(i + " is even");4 }5}| i | i % 2 | Condition | Output |
|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Multiple Accumulators
1let evens = 0;2let odds = 0;3 4for (let i = 1; i <= 6; i++) {5 if (i % 2 === 0) {6 evens = evens + i;7 } else {8 odds = odds + i;9 }10}11 12console.log("Evens:", evens, "Odds:", odds);| i | i % 2 | evens (after) | odds (after) |
|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Find the Maximum
1let numbers = [3, 7, 2, 9, 4];2let max = numbers[0];3 4for (let i = 1; i < numbers.length; i++) {5 if (numbers[i] > max) {6 max = numbers[i];7 }8}9 10console.log(max);| i | numbers[i] | max (before) | Condition | max (after) |
|---|---|---|---|---|
What will this code print? Commit to your answer before revealing.
Computational empathy moment: This pattern — initialize, then iterate — is fundamental. The machine needs a starting point before it can compare.