-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiamondStars.js
42 lines (37 loc) · 932 Bytes
/
DiamondStars.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
/*
Problem Statement: (Secureworks Dell Round 2)
Need to print an output like :
*
***
*****
*******
*****
***
*
'n'here will be 4.
Achieve this.
Run this script in Browser by inputing a dummy HTML file with a script tag.
*/
const PrintStars = (n = 1) => {
if(typeof n !== 'number' || n < 1) {
return;
}
n = parseInt(n,10);
const arrLen = n + (n - 1);
let counterOfStars = 1;
mode = 'inc';
for (let i = 0; i < arrLen; i++) {
const noOfSpBA = (arrLen - counterOfStars) / 2;
const starSpace = (new Array(noOfSpBA).fill(' ')).concat(new Array(counterOfStars).fill('*'));
console.log(starSpace.join(''))
if (counterOfStars === arrLen) {
mode = 'dec';
counterOfStars = counterOfStars - 2;
} else if (mode === 'inc') {
counterOfStars = counterOfStars + 2;
} else {
counterOfStars = counterOfStars - 2;
}
}
}
PrintStars(5)