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
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.

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.
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.
Your check results
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.
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.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
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.
function printTriangle(maxLevels) {
let triangleBlock = "X";
for (let currentLevel = 0; currentLevel < maxLevels; currentLevel++) {
console.log(triangleBlock);
triangleBlock += "X";
}
}
printTriangle(10);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

7 days freeTry The Code Zone
THE CODE ZONE COMMUNITY
Your next step is easier together.
Try the community free for 7 days.
No charge during your trial. Choose your plan on Skool.
- Ask questions and get support in the community
- Watch every course video walkthrough
- Find a study buddy in the 24/7 study room
Make the most of your trial with Premium.
Choose Premium on Skool to try Thursday live help sessions. Bring your questions, work through code, and get unstuck together.
Scheduled Thursdays in Eastern Time. Check the Skool calendar for your local date and time.
Have a website account? Sign in