Skip to content

Commit 1ae8a5a

Browse files
author
Coding Money
committed
pyramid
1 parent 6e122ff commit 1ae8a5a

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

Diff for: completed_exercises/8-steps-string-pattern.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// --- Directions
2+
// Write a function that accepts a positive number N.
3+
// The function should console log a step shape
4+
// with N levels using the # character. Make sure the
5+
// step has spaces on the right hand side!
6+
// --- Examples
7+
// steps(2)
8+
// '# '
9+
// '##'
10+
// steps(3)
11+
// '# '
12+
// '## '
13+
// '###'
14+
// steps(4)
15+
// '# '
16+
// '## '
17+
// '### '
18+
// '####'
19+
20+
function steps(n) {
21+
for(let row=1; row<=n; row++){
22+
let line = ''
23+
for(let col=1; col<=n; col++){
24+
if(col<=row){
25+
line += '#'
26+
}else{
27+
line += ' '
28+
}
29+
}
30+
console.log(line)
31+
}
32+
}
33+
34+
steps(6)

Diff for: completed_exercises/9-pyramid.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
function pyramid(n) {
18+
const mid = Math.floor((2*n-1)/2)
19+
for(let row=0; row<n; row++){
20+
let line = ''
21+
for(let col=0; col<2*n-1; col++){
22+
if( col >= mid - row && col<= mid + row ){
23+
line += '#'
24+
}else{
25+
line += ' '
26+
}
27+
}
28+
console.log(line)
29+
}
30+
}
31+
32+
pyramid(9)

Diff for: exercises/8-steps-string-pattern.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// --- Directions
2+
// Write a function that accepts a positive number N.
3+
// The function should console log a step shape
4+
// with N levels using the # character. Make sure the
5+
// step has spaces on the right hand side!
6+
// --- Examples
7+
// steps(2)
8+
// '# '
9+
// '##'
10+
// steps(3)
11+
// '# '
12+
// '## '
13+
// '###'
14+
// steps(4)
15+
// '# '
16+
// '## '
17+
// '### '
18+
// '####'
19+
20+
function steps(n) {}
21+
22+
steps(3)

Diff for: exercises/9-pyramid.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
function pyramid(n) {}
18+
19+
pyramid(3)

0 commit comments

Comments
 (0)