[Solved] Triangle of Stars - JavaScript task

  

3
Topic starter

Write a JS function that outputs a triangle made of stars with variable size, depending on an input parameter. Look at the examples to get an idea. The input comes as a single number argument.

Examples:

Input:

5

Output:

javatr

The output is a series of lines printed on the console, forming a triangle of variable size.

1 Answer
2

Here is my js program. Note line 10 - the reverse loop:

function printTriangle(n) {
    function printStars(count=n) {//by default count=n
        console.log("*".repeat(count));
    }
 
    for (let i = 1; i <= n; i++) {
        printStars(i);
    }
 
    for (let i = n - 1; i > 0; i--) {//reverse loop
        printStars(i);
    }
}
 
printTriangle(6);
Share: