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 3a69741

Browse files
Implement steps solutions - one of them with recursion, write tests
1 parent 059923d commit 3a69741

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

‎09_steps/index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
// Solution 1
21+
// function steps(n) {
22+
// if(n === 1) {
23+
// console.log("#");
24+
// } else {
25+
// for(let s = 1; s <= n; s++) {
26+
// console.log('#'.repeat(s) + ' '.repeat(n - s))
27+
//
28+
// }
29+
// }
30+
// }
31+
32+
// Solution 2
33+
34+
// function steps(n) {
35+
// for(let row = 0; row < n; row++) {
36+
// let stair = "";
37+
//
38+
// for (let column = 0; column < n; column ++) {
39+
// if(column <= row) {
40+
// stair += '#';
41+
// } else {
42+
// stair += ' ';
43+
// }
44+
// }
45+
// console.log(stair);
46+
// }
47+
// }
48+
49+
function steps(n, row = 0, stair= '') {
50+
if (n === row) {
51+
return;
52+
}
53+
if(n === stair.length) {
54+
console.log(stair);
55+
return steps(n, row + 1);
56+
} else {
57+
if(stair.length <= row) {
58+
stair += '#'
59+
} else {
60+
stair += ' '
61+
}
62+
return steps(n, row, stair);
63+
}
64+
}
65+
66+
module.exports = steps;

‎09_steps/test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const steps = require('./index');
2+
3+
beforeEach(() => {
4+
jest.spyOn(console, 'log');
5+
});
6+
7+
afterEach(() => {
8+
console.log.mockRestore();
9+
});
10+
11+
test('steps is a function', () => {
12+
expect(typeof steps).toEqual('function');
13+
});
14+
15+
test('steps called with n = 1', () => {
16+
steps(1);
17+
expect(console.log.mock.calls[0][0]).toEqual('#');
18+
expect(console.log.mock.calls.length).toEqual(1);
19+
});
20+
21+
test('steps called with n = 2', () => {
22+
steps(2);
23+
expect(console.log.mock.calls[0][0]).toEqual('# ');
24+
expect(console.log.mock.calls[1][0]).toEqual('##');
25+
expect(console.log.mock.calls.length).toEqual(2);
26+
});
27+
28+
test('steps called with n = 3', () => {
29+
steps(3);
30+
expect(console.log.mock.calls[0][0]).toEqual('# ');
31+
expect(console.log.mock.calls[1][0]).toEqual('## ');
32+
expect(console.log.mock.calls[2][0]).toEqual('###');
33+
expect(console.log.mock.calls.length).toEqual(3);
34+
});

0 commit comments

Comments
(0)

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