-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPattern_20.js
44 lines (36 loc) · 851 Bytes
/
Pattern_20.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function butterflyPattern(n) {
// Outer loop for number of rows/lines
for (let i = 1; i < 2 * n; i++) {
let stars, spaces;
// Calculate stars and spaces based on the row number
// For upper half of the pattern
if (i <= n) {
stars = i;
spaces = 2 * n - 2 * i;
}
// for lower half of the pattern
else {
stars = 2 * n - i;
spaces = 2 * i - 2 * n;
}
let row = '';
// Print stars
for (let j = 1; j <= stars; j++) {
row += '*';
}
// Print spaces
for (let j = 1; j <= spaces; j++) {
row += ' ';
}
// Print stars again
for (let j = 1; j <= stars; j++) {
row += '*';
}
// Output the row
console.log(row);
}
}
// call the function
// butterflyPattern(5);
// Export the function for testing
module.exports = butterflyPattern;