-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstairs.js
More file actions
77 lines (61 loc) · 1.38 KB
/
stairs.js
File metadata and controls
77 lines (61 loc) · 1.38 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function steps(n){
for (let row = 0; row < n ; row++){
let stair = ''
for(let column = 0; column < n ; column++){
if (column <= row){
stair+= '#'
} else {
stair += ' '
}
}
console.log(stair)
}
}
function steps(n,row = 0,stair = ''){
if (n === row){
return;
}
if (n === stair.length){
console.log(stair)
return steps(n,row+1)
}
if (stair.length <= row){
stair+= '#'
} else {
stair += ' ';
}
steps(n,row,stair)
}
steps(4)
function pyramid(n){
let middle = Math.floor((n * 2 - 1) / 2)
for (let row = 0; row < n ; row++){
let pyramid = ''
for(let column = 0; column < 2 * n - 1 ; column++){
if (middle - row <= column && middle + row >= column){
pyramid += '#'
} else {
pyramid += ' '
}
}
console.log(pyramid)
}
}
function pyramid(n, row = 0, level = '') {
if (row === n) {
return;
}
if (level.length === 2 * n - 1) {
console.log(level);
return pyramid(n, row + 1);
}
const midpoint = Math.floor((2 * n - 1) / 2);
let add;
if (midpoint - row <= level.length && midpoint + row >= level.length) {
add = '#';
} else {
add = ' ';
}
pyramid(n, row, level + add);
}
pyramid(3)