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 63d0550

Browse files
Implement a solution for fizzbuzz problem, write tests
1 parent b2895ac commit 63d0550

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

‎05_fizzbuzz/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// --- Directions
2+
// Write a program that console logs the numbers
3+
// from 1 to n. But for multiples of three print
4+
// "fizz" instead of the number and for the multiples
5+
// of five print "buzz". For numbers which are multiples
6+
// of both three and five print "fizzbuzz".
7+
// --- Example
8+
// fizzBuzz(5);
9+
// 1
10+
// 2
11+
// fizz
12+
// 4
13+
// buzz
14+
15+
function fizzBuzz(n) {
16+
for (let i = 1; i <= n; i++) {
17+
// Is the number multiple of 3 and 5?
18+
if(i % 3 === 0 && i % 5 === 0) {
19+
console.log('fizzbuzz');
20+
// Is the number multiple of 3?
21+
} else if (i % 3 === 0) {
22+
console.log('fizz');
23+
// Is the number multiple of 5?
24+
} else if (i % 5 === 0) {
25+
console.log('buzz');
26+
} else {
27+
console.log(i);
28+
}
29+
}
30+
}
31+
32+
module.exports = fizzBuzz;

‎05_fizzbuzz/test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const fizzBuzz = require('./index');
2+
3+
test('fizzBuzz function is defined', () => {
4+
expect(fizzBuzz).toBeDefined();
5+
});
6+
7+
test('Calling fizzbuzz with `5` prints out 5 statements', () => {
8+
fizzBuzz(5);
9+
10+
expect(console.log.mock.calls.length).toEqual(5);
11+
});
12+
13+
test('Calling fizzbuzz with 15 prints out the correct values', () => {
14+
fizzBuzz(15);
15+
16+
expect(console.log.mock.calls[0][0]).toEqual(1);
17+
expect(console.log.mock.calls[1][0]).toEqual(2);
18+
expect(console.log.mock.calls[2][0]).toEqual('fizz');
19+
expect(console.log.mock.calls[3][0]).toEqual(4);
20+
expect(console.log.mock.calls[4][0]).toEqual('buzz');
21+
expect(console.log.mock.calls[5][0]).toEqual('fizz');
22+
expect(console.log.mock.calls[6][0]).toEqual(7);
23+
expect(console.log.mock.calls[7][0]).toEqual(8);
24+
expect(console.log.mock.calls[8][0]).toEqual('fizz');
25+
expect(console.log.mock.calls[9][0]).toEqual('buzz');
26+
expect(console.log.mock.calls[10][0]).toEqual(11);
27+
expect(console.log.mock.calls[11][0]).toEqual('fizz');
28+
expect(console.log.mock.calls[12][0]).toEqual(13);
29+
expect(console.log.mock.calls[13][0]).toEqual(14);
30+
expect(console.log.mock.calls[14][0]).toEqual('fizzbuzz');
31+
});
32+
33+
beforeEach(() => {
34+
jest.spyOn(console, 'log');
35+
});
36+
37+
afterEach(() => {
38+
console.log.mockRestore();
39+
});

0 commit comments

Comments
(0)

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