Everyone gets stuck, and the point of practice is to get better at getting unstuck. This is the routine the practice playground is built around.
1. Read the failing test
A failing test tells you three things: the input, the expected answer and what your code gave. Read all three slowly. Compare them before you change any code. Often the difference is obvious once you look: an array one item short, a number as a string, a capital letter.
2. Trace one input by hand
Take the smallest failing input and play computer. Write each variable on paper and update it line by line. For a loop, write one row per pass. The moment your paper and your expectation disagree is where the bug is.
i result
1 ["1"]
2 ["1", "2"]
3 ["1", "2", "Fizz"]3. Print what you cannot see
If tracing by hand is slow, let the computer do it. Add a console.log inside the loop to print the variables you care about. The playground shows that output under the test results. Remove the prints when you are done.
4. Shrink the problem
If the whole task feels too big, solve a smaller one first. For FizzBuzz, return just the numbers as strings. Then add "Fizz". Then "Buzz". Each step passes more tests, and you always know which change broke something.
5. Use hints one at a time
Hints work best as a ladder. Read the first one, close it, and try again before asking for the next. On this site:
- What the tests saw appears after a run. It is chosen from what your function returned, so it points at your actual mistake, such as a missing return or checking 3 before 15.
- Get a hint reveals the exercise's own hints in order, from a general nudge to the specific method.
- Show one possible solution appears only after the last hint. If you use it, close it, wait a day and solve the exercise again from a blank editor.
A worked example
Here is the routine on a real bug. The task is to return the largest number in a list, and this code passes three tests but fails one:
function largest(numbers) {
let best = 0;
for (const n of numbers) {
if (n > best) best = n;
}
return best;
}Read the test: largest([-5, -2, -8]) expected -2, got 0. Trace it: best starts at 0. Is -5 bigger than 0? No. -2? No. -8? No. So best is still 0, a number that is not even in the list. Fix: start best at the first item, numbers[0], so the answer always comes from the list. One trace, one change, and every test passes.
Questions to ask yourself
- What exactly does the function receive, and what must it return?
- Which test fails, and what is special about its input?
- Does my loop start and stop where I think it does?
- Am I returning the value, or only printing it?
- Am I comparing the right types, such as a number with a string?
6. Take a short break
After 20 minutes without progress, step away for five. Fresh eyes spot typos and wrong assumptions that tired ones skip over. Explaining the problem out loud, even to no one, has the same effect.
After you solve it
Spend one more minute on it. Can you make it shorter or clearer? Is there an input you did not think about, such as an empty string or a negative number? Those edge cases are what the last test in each exercise usually checks. The error messages guide covers the problems you will meet most.
Key points
- A failing test shows the input, the expected answer and what you got. Read all three first.
- Trace the smallest failing input by hand, one line at a time.
- Print the values you cannot see, then remove the prints.
- Solve a smaller version first and grow it test by test.
- Take hints one at a time, and try something between each one.
- After 20 minutes without progress, take a five-minute break. Being stuck is part of learning, not a sign you are bad at it; every programmer spends time stuck, and the routine above is how they get moving again.
Try it on a real exercise
Write a function, run the tests, get a hint if it fails.