Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0974a56

Browse files
Adding pyramid
1 parent 8869a10 commit 0974a56

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

‎pyramid/directions.md‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# --- Directions
2+
3+
Write a function that accepts a positive number N.
4+
The function should console log a pyramid shape
5+
with N levels using the # character. Make sure the
6+
pyramid has spaces on both the left *and* right hand sides
7+
8+
#--- Examples
9+
10+
* pyramid(1) should return:
11+
'#'
12+
13+
* pyramid(2) should return:
14+
' # '
15+
'###'
16+
17+
* pyramid(3) should return:
18+
' # '
19+
' ### '
20+
'#####'
21+
22+
23+
# --- Solutions
24+
25+
// Solution No 1
26+
27+
function pyramid(n) {
28+
const midpoint = Math.floor((2*n-1)/2);
29+
for(let row = 0; row < n; row++) {
30+
let level = '';
31+
32+
for(let column = 0; column < 2*n-1; column++) {
33+
if(midpoint - row <= column && midpoint + row >= column){
34+
level += '#';
35+
} else {
36+
level += ' ';
37+
}
38+
}
39+
console.log(level);
40+
}
41+
}
42+
43+
44+
// Solution No 2
45+
46+
function pyramid(n, row = 0, level = '') {
47+
if(n === row){
48+
return;
49+
}
50+
51+
if(level.length === 2*n-1){
52+
console.log(level);
53+
return pyramid(n, row+1);
54+
}
55+
56+
const midpoint = Math.floor((2*n-1)/2);
57+
let add;
58+
if(midpoint - row <= level.length && midpoint + row >= level.length){
59+
add = '#';
60+
} else {
61+
add = ' ';
62+
}
63+
64+
pyramid(n, row, level+add);
65+
}

‎pyramid/pyramid.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// --- Directions
2+
// Write a function that accepts a positive number N.
3+
// The function should console log a pyramid shape
4+
// with N levels using the # character. Make sure the
5+
// pyramid has spaces on both the left *and* right hand sides
6+
// --- Examples
7+
// pyramid(1)
8+
// '#'
9+
// pyramid(2)
10+
// ' # '
11+
// '###'
12+
// pyramid(3)
13+
// ' # '
14+
// ' ### '
15+
// '#####'
16+
17+
18+
// Solution No 1
19+
20+
function pyramid(n) {
21+
const midpoint = Math.floor((2*n-1)/2);
22+
for(let row = 0; row < n; row++) {
23+
let level = '';
24+
25+
for(let column = 0; column < 2*n-1; column++) {
26+
if(midpoint - row <= column && midpoint + row >= column){
27+
level += '#';
28+
} else {
29+
level += ' ';
30+
}
31+
}
32+
console.log(level);
33+
}
34+
}
35+
36+
// Solution No 2
37+
38+
function pyramid(n, row = 0, level = '') {
39+
if(n === row){
40+
return;
41+
}
42+
43+
if(level.length === 2*n-1){
44+
console.log(level);
45+
return pyramid(n, row+1);
46+
}
47+
48+
const midpoint = Math.floor((2*n-1)/2);
49+
let add;
50+
if(midpoint - row <= level.length && midpoint + row >= level.length){
51+
add = '#';
52+
} else {
53+
add = ' ';
54+
}
55+
56+
pyramid(n, row, level+add);
57+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /