settingsAccountsettings
Menusettings

Q: Print figure of 4 Squares in JavaScript

+1 vote

Write JavaScript function to print a figure of 4 squares of size n like shown in the examples below.

The input is an integer number n in the range [2 … 200].

Examples:

print square in javascript

The output consists of n lines for odd n and n-1 lines for even n. Each line holds 2*n-1 characters (+ or | or space) as shown in the examples. The figure is fully symmetric (horizontally and vertically).

asked in JavaScript category by user matthew44

1 Answer

0 votes

Here is the solution (use nested loops and if-statements. Try to figure out the logic of construction of the above figures):

function figure(n) {
    let length = n % 2 !== 0 ? n : n - 1;
    let count = (2 * n - 4) / 2;
    let middle = Math.ceil(length / 2);
    for (let i = 1; i <= length; i++) {
        if (i == 1 || i == middle || i == length) {
            console.log(`+${'-'.repeat(count)}+${'-'.repeat(count)}+`);
        } else {
            console.log(`|${' '.repeat(count)}|${' '.repeat(count)}|`);
        }
    }
}

figure(8);

 

answered by user sam
...