Skip to content

Commit 723a5b2

Browse files
author
Connor Leech
committed
add pyramids recursive
1 parent d8779bd commit 723a5b2

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

udemy-interview-bootcamp-course/pyramids.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
// '#####'
1616

1717
function pyramids(height){
18-
const midpoint = Math.floor((height + height - 1) / 2);
18+
const midpoint = Math.floor((2 * height - 1) / 2);
1919

2020

2121
for(var row = 0; row < height; row++){
2222
let block = '';
2323

24-
for(var column = 0; column < height + height - 1; column++){
24+
for(var column = 0; column < 2 * height - 1; column++){
2525

2626
block += (midpoint - row <= column && midpoint + row >= column) ? '#' : ' ';
2727
}
@@ -31,9 +31,25 @@ function pyramids(height){
3131
}
3232

3333

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);
3652

3753
}
3854

39-
pyramids(3);
55+
pyramidsRecursive(3);

0 commit comments

Comments
 (0)