File tree Expand file tree Collapse file tree 1 file changed +21
-5
lines changed
udemy-interview-bootcamp-course Expand file tree Collapse file tree 1 file changed +21
-5
lines changed Original file line number Diff line number Diff line change 15
15
// '#####'
16
16
17
17
function pyramids ( height ) {
18
- const midpoint = Math . floor ( ( height + height - 1 ) / 2 ) ;
18
+ const midpoint = Math . floor ( ( 2 * height - 1 ) / 2 ) ;
19
19
20
20
21
21
for ( var row = 0 ; row < height ; row ++ ) {
22
22
let block = '' ;
23
23
24
- for ( var column = 0 ; column < height + height - 1 ; column ++ ) {
24
+ for ( var column = 0 ; column < 2 * height - 1 ; column ++ ) {
25
25
26
26
block += ( midpoint - row <= column && midpoint + row >= column ) ? '#' : ' ' ;
27
27
}
@@ -31,9 +31,25 @@ function pyramids(height){
31
31
}
32
32
33
33
34
- function pyramidsRecursive ( height ) {
35
-
34
+ function pyramidsRecursive ( height , row = 0 , level = '' ) {
35
+ if ( row === height ) return ;
36
+
37
+ if ( level . length === 2 * height - 1 ) {
38
+ console . log ( level ) ;
39
+ return pyramidsRecursive ( height , row + 1 ) ;
40
+ }
41
+
42
+ const midpoint = Math . floor ( ( 2 * height - 1 ) / 2 ) ;
43
+ let add ;
44
+
45
+ if ( midpoint - row <= level . length && midpoint + row >= level . length ) {
46
+ add = '#' ;
47
+ } else {
48
+ add = ' ' ;
49
+ }
50
+
51
+ pyramidsRecursive ( height , row , level + add ) ;
36
52
37
53
}
38
54
39
- pyramids ( 3 ) ;
55
+ pyramidsRecursive ( 3 ) ;
You can’t perform that action at this time.
0 commit comments