015 · Selection, chance and loops / Practice · 20 min
Build a triangle one row at a time
The original prompt uses both triangle and pyramid without specifying spaces or widths. This version chooses a left-aligned growing triangle and shows the exact rows. It does not claim the 17-second source clip demonstrates this exact layout.
Activity 5 of 5 in Selection, chance and loops

ORIGINAL WALKTHROUGH · 05:05
Watch with Rob.
Try the challenge first. When you need help, watch Rob work through it, pause to try a change, then check your own code below.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Your challenge
Complete printTriangle(maxLevels) with a for loop. This browser adaptation defines the requested triangle precisely: print left-aligned rows containing 1 X, then 2 X, and so on up to maxLevels X, with no spaces or blank lines. Keep levels = 10 and the supplied call for your first run. Zero levels prints nothing. A centered pyramid is an optional later variation, not this checked shape.
Keep the supplied input variables at the top. The checks change only those initial values in a separate sandbox and compare the result. They also check the explicitly requested code structures. Your own editor and saved draft are not changed by the scenario checks.
Give the vague shape a concrete contract
The original prompt uses both triangle and pyramid without specifying spaces or widths. This version chooses a left-aligned growing triangle and shows the exact rows. It does not claim the 17-second source clip demonstrates this exact layout.
The parameter controls the loop
Use maxLevels inside the function, not a hardcoded 10. Start the row at 1 and stop after maxLevels. repeat(row) creates the correct width for the current row.
Run, check and explain
Press Run code to see your current output. Choose Check challenge for named feedback on the starting case and variations. After three failed checks—or immediately after passing—you can reveal an explained example without replacing your code.
Signed-out work stays on this browser. Signed-in learners can save an account draft, submit for review and share a separate help snapshot. A passed browser check is practice feedback, not an instructor-certified grade.
YOUR TURN
Print a triangle
Complete printTriangle(maxLevels) with a for loop. This browser adaptation defines the requested triangle precisely: print left-aligned rows containing 1 X, then 2 X, and so on up to maxLevels X, with no spaces or blank lines. Keep levels = 10 and the supplied call for your first run. Zero levels prints nothing. A centered pyramid is an optional later variation, not this checked shape.
Checks run separate copies with supplied inputs; your editor stays unchanged.
- Define the requested function
- Use a for loop
- Ten levels produce ten growing rows
- Three levels are X, XX, XXX
- One level prints one X
- Zero levels prints no rows
Sample output for the starting inputs
X XX XXX XXXX XXXXX XXXXXX XXXXXXX XXXXXXXX XXXXXXXXX XXXXXXXXXX
Random values are supplied during checks. Run code uses real randomness.
Run code displays your current output. Check challenge checks the named scenarios and requested structures in a separate sandbox. Save account draft keeps your code across devices when signed in. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
The row number is its width
For row 3, print "X".repeat(3).
Keep the function reusable
The checks change levels; the function must follow its parameter.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
The original prompt uses both triangle and pyramid without specifying spaces or widths. This version chooses a left-aligned growing triangle and shows the exact rows. It does not claim the 17-second source clip demonstrates this exact layout. Use maxLevels inside the function, not a hardcoded 10. Start the row at 1 and stop after maxLevels. repeat(row) creates the correct width for the current row.
const levels = 10;
function printTriangle(maxLevels) {
for (let row = 1; row <= maxLevels; row++) {
console.log("X".repeat(row));
}
}
printTriangle(levels);