thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

22.04 · Final JavaScript challenges / Practice · 8 min

Include the final triangle row

Grow one line at a time and print exactly the requested number of levels.

Activity 4 of 8 in Final JavaScript challenges

All courses

JavaScript Basics / 22.04

Include the final triangle row

Not started

Grow one line at a time and print exactly the requested number of levels.

More options
Rob finds only two rows for a three-level task, then adds the missing XXX row.
Rob’s comic strip · Final JavaScript challenges

Print a triangle one row at a time

Work through 4 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

4 steps · Saved work · Instant feedback

YOUR TURN

Include the final triangle row

Repair printTriangle(maxLevels) so printTriangle(10) prints exactly 10 lines: X, XX, and so on through XXXXXXXXXX. Start with one X and add one after each printed row. Keep the function parameter and call; do not hard-code ten output lines. For a nonnegative whole-number input n, print n rows, and print nothing when n is 0. No spaces or blank extra lines.

Expected output
X
XX
XXX
XXXX
XXXXX
XXXXXX
XXXXXXX
XXXXXXXX
XXXXXXXXX
XXXXXXXXXX

Keep the starting inputs. Print these lines in this order, without extra output.

Edit JavaScript, then run it. Check Challenge shows which output checks pass or need a fix. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
function printTriangle(maxLevels) {
  let triangleBlock = "X";
  for (let currentLevel = 0; currentLevel < maxLevels - 1; currentLevel++) {
    console.log(triangleBlock);
    triangleBlock += "X";
  }
}
printTriangle(10);

Need a nudge?

Count the iterations

Starting at 0, the counters 0 through 9 make ten iterations. Comparing against maxLevels - 1 stops one row too early.

Print, then grow

Print triangleBlock before appending the next X. Otherwise the first row will already contain two Xs.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

The counter tracks completed iterations; triangleBlock holds the next row to print. With maxLevels = 10, the loop runs for counters 0–9 and the tenth row has ten Xs. The original written output omitted that final row; the companion includes it. Try levels 0, 1 and 4 as well.