LoopsX Studio
No. 26 / Advanced

X Pattern

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

A (2n − 1) × (2n − 1) grid. A cell is a star when it sits on one of the two diagonals: j = i or j = 2n − i.

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