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 0065648

Browse files
feat: fizzBuzz
1 parent 7f8bbb1 commit 0065648

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

‎src/5_fizzBuzz/fizzBuzz.test.ts‎

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

‎src/5_fizzBuzz/index.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// --- Task
2+
// Write a function 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+
8+
// --- Example
9+
// fizzBuzz(5);
10+
// 1
11+
// 2
12+
// fizz // n / 3 === 0
13+
// 4
14+
// buzz // 5 / 5 === 0
15+
16+
17+
export const fizzBuzz = (n: number): void => {
18+
for (let i = 1; i <= n; i++) {
19+
if (i % 15 === 0) {
20+
console.log('fizzbuzz');
21+
} else if (i % 5 === 0) {
22+
console.log('buzz');
23+
} else if (i % 3 === 0) {
24+
console.log('fizz');
25+
} else console.log(i);
26+
}
27+
};

0 commit comments

Comments
(0)

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