settingsAccountsettings
Menusettings

Q: Rectangle of Stars - JavaScript task

+3 votes

Write a JavaScript function that outputs a rectangle made of stars with variable size, depending on an input parameter. If there is no parameter specified, the rectangle should always be of size 5. Look at the examples to get an idea. The input comes as a single number argument.

Examples:

Input:
1

Output:

*

Input:
2

Output:

**
**

Input:
5

Output:

*****
*****
*****
*****
*****

Input:


Output:

*****
*****
*****
*****
*****

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

asked in JavaScript category by user andrew

2 Answers

+2 votes

Here is my JS rectangle:

function squareOfStars(n) {
    function printStars(count = n) {
        console.log("*" + " *".repeat(count - 1));
    }

    for (let i = 1; i <= n; i++) {
        console.log("*" + " *".repeat(n - 1));
        printStars();
    }
}

squareOfStars(10);
answered by user ak47seo
0 votes

mine rectangle is easier to understand:

function printSq(n) {
    for (let i = 1; i <= n; i++) {
        console.log("*" + " *".repeat(n - 1));
    }
}

printSq(40);
answered by user eiorgert
...