-
Notifications
You must be signed in to change notification settings - Fork 0
/
rightTrianglePattern.js
56 lines (42 loc) · 1.43 KB
/
rightTrianglePattern.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
45
46
47
48
49
50
51
52
53
54
55
56
function rightTrianglePattern1(rows) {
let pattern = '';
function generateString(count, totalLength) {
let string = '';
for (let i = 0; i < totalLength; i++) {
string += i < count ? "*" : ' ';
}
return string;
}
for (let i = 1; i <= rows; i++) {
pattern += (generateString(i, rows) + (i < rows ? '\n' : ''));
}
return pattern;
}
function rightTrianglePattern2(rows) {
let pattern = '';
function generateString(count, totalLength) {
return Array.from(Array(totalLength)).map((_, index) => index < count ? '#' : ' ').join('');
}
for (let i = 1; i <= rows; i++) {
pattern += (generateString(i, rows) + (i < rows ? '\n' : ''));
}
return pattern;
}
/* Demonstration:- Let rows = 3
Ist Outer Loop => i = 1
Run Inner Loop from 0 to 3 (excluding) with star count as 1;
<= '* ' { 0 < 1: '*', 1 < 1: ' ', 2 < 1: ' ' }
∴ pattern = |* |
IInd Outer Loop => i = 2
Run Inner Loop from 0 to 3 (excluding) with star count as 2;
<= '** ' { 0 < 2: '*', 1 < 2: '*', 2 < 2: ' ' }
∴ pattern = |* |
|** |
IIIrd Outer Loop => i = 1
Run Inner Loop from 0 to 3 (excluding) with star count as 3;
<= '***' { 0 < 3: '*', 1 < 3: '*', 2 < 3: '*' }
∴ pattern = |* |
|** |
|***|
*/
module.exports = { rightTrianglePattern1, rightTrianglePattern2 }