[해결로 표시] 별의 삼각형 - JavaScript 작업

  

3
주제 스타터

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.

예시:

입력:

5

출력:

javatr

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

1개 답글
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);
공유: