-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.js
More file actions
43 lines (37 loc) · 1.09 KB
/
triangle.js
File metadata and controls
43 lines (37 loc) · 1.09 KB
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
const main = function(){
let triangleAlignment = process.argv[2];
let height = +process.argv[3];
let filler = "*";
let isLeftAligned = (triangleAlignment == "left");
let isRightAligned = (triangleAlignment == "right");
let triangle = drawLeftTriangle(height, filler);
if(isRightAligned){
triangle = drawRightTriangle(height, filler);
}
console.log(triangle);
}
const generateLine = function (symbol, length){
let line = "";
for(let count=0; count<length; count++){
line += symbol;
}
return line;
}
const drawLeftTriangle = function(height, filler){
let triangle = "";
for(let lineLength=1; lineLength<height; lineLength++){
triangle += generateLine(filler, lineLength) + "\n";
}
triangle += generateLine(filler, height);
return triangle;
}
const drawRightTriangle = function(height, filler){
let triangle = "";
for(let lineLength=1; lineLength<height; lineLength++){
triangle += generateLine(" ", height - lineLength);
triangle += generateLine(filler, lineLength) + "\n";
}
triangle += generateLine(filler, height);
return triangle;
}
main();