-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw-a-triangle.js
48 lines (39 loc) · 1.28 KB
/
draw-a-triangle.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
// Draw a Triangle
/*
Create a JavaScript function drawTriangle(n) that takes an integer n as input and generates a triangle pattern using asterisks (*). The triangle should have n rows, with each row containing an increasing number of asterisks, starting from one asterisk in the first row and ending with n asterisks in the last row. The function should return the generated triangle as a string.
Challenge:
Write a JavaScript function drawTriangle(n) that outputs a triangle pattern of asterisks with n rows. Ensure that each row has the appropriate number of asterisks according to its position in the triangle. For example, if n is 5, the output should be:
*
**
***
****
*****
Your function should handle edge cases gracefully, such as when n is 0 or negative. It should also return an empty string when n is 0. Test your function with different values of n to ensure its correctness.
*/
// function drawTriangle(n){
// let str = '';
// let result = '';
// for(let i = 0; i < n; i++){
// str += '*';
// result += str + '\n';
// }
// return result;
// }
const drawTriangle = n => {
let res = '';
for (let i = 1; i <= n; i++) {
for (let j = 0; j < i; j++) {
res += '*';
}
res += '\n';
}
return res;
}
console.log(drawTriangle(5))
/*
*
**
***
****
*****
*/