No. 37 / Advanced
N Pattern
Outputn = 5
* * ** * * * * * ** * *
5 rows
Controls
5
Character
Normal
How it works
An n × n grid. A cell is a star in the first column, in the last column, or on the main diagonal j = i.
rows ncolumns nstar when j == 1 || j == n || j == i
pattern.js
1
const n = 5;2
3
for (let i = 1; i <= n; i++) {4
let row = "";5
for (let j = 1; j <= n; j++) {6
if (j === 1 || j === n || j === i) {7
row += "*";8
} else {9
row += " ";10
}11
}12
console.log(row);13
}