-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
57 lines (47 loc) · 954 Bytes
/
index.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
57
#!/usr/bin/env node
/**
* @param {int} n
* @returns {string}
*/
function triangle(n) {
const buf = [];
for (let i = 1; i <= n; i++) {
buf.push("*".repeat(i));
}
return buf.join("\n");
}
/**
*
* @param {int} n
* @returns {string}
*/
function middleTriangle(n) {
const buf = [];
const middle = Math.floor((2 * n - 1) / 2);
for (let i = 0; i < n; i++) {
const spaceCount = middle - i;
const space = " ".repeat(Math.max(spaceCount, 0));
const star = "*".repeat(2 * i + 1)
buf.push(space + star);
}
return buf.join("\n");
}
/**
*
* @param {int} n
* @returns {string}
*/
function tree(n) {
const middle = Math.floor((2 * n - 1) / 2);
return middleTriangle(n) + `\n${" ".repeat(middle)}|`.repeat(n);
}
/**
* Fib
*/
function fib(n) {
if (n < 1) return 1;
return n + fib(n-1);
}
console.log(triangle(10))
console.log(tree(10))
console.log(fib(10))