LoopsX Studio
No. 36 / Advanced

Z Pattern

Outputn = 5
*****
   * 
  *  
 *   
*****
5 rows
Controls
5
Character
Normal
How it works

An n × n grid. A cell is a star in the first row, in the last row, or on the anti-diagonal i + j = n + 1.

rows ncolumns nstar when i == 1 || i == n || i + j == n + 1
pattern.js
1const n = 5;
2
3for (let i = 1; i <= n; i++) {
4 let row = "";
5 for (let j = 1; j <= n; j++) {
6 if (i === 1 || i === n || i + j === n + 1) {
7 row += "*";
8 } else {
9 row += " ";
10 }
11 }
12 console.log(row);
13}