Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

Commit 4ece057

Browse files
committed
Add another exercise - forEach
This involves a bit of mocking of the console global, as otherwise it's difficult to test side effects which is somewhat necessary with forEach (otherwise why not just use a map?)
1 parent 4df079d commit 4ece057

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

mandatory/12-fizz-buzz.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Write a function that accepts an array of numbers as a parameter and loops through the numbers.
3+
For each number the function should print (using console.log):
4+
5+
- If the number is a multiple of 3, print “Fizz” instead of the number
6+
- If the number is a multiple of 5, print “Buzz” instead of the number
7+
- If the number is a multiple of *both* 3 and 5, print “FizzBuzz” instead of the number
8+
- If the number is none of the above, just print the number
9+
*/
10+
11+
function fizzBuzz(numbers) {}
12+
13+
/*
14+
===================================================
15+
======= TESTS - DO NOT MODIFY BELOW THIS LINE =====
16+
There are some Tests in this file that will help you work out if your code is working.
17+
To run the tests for just this one file, type `npm test -- --testPathPattern 12-fizz-buzz` into your terminal
18+
(Reminder: You must have run `npm install` one time before this will work!)
19+
===================================================
20+
*/
21+
22+
test("fizzBuzz works", () => {
23+
const log = jest.spyOn(console, "log").mockImplementation(() => {});
24+
25+
fizzBuzz([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
26+
27+
expect(log).toHaveBeenNthCalledWith(1, 1);
28+
expect(log).toHaveBeenNthCalledWith(2, 2);
29+
expect(log).toHaveBeenNthCalledWith(3, "Fizz");
30+
expect(log).toHaveBeenNthCalledWith(4, 4);
31+
expect(log).toHaveBeenNthCalledWith(5, "Buzz");
32+
expect(log).toHaveBeenNthCalledWith(6, "Fizz");
33+
expect(log).toHaveBeenNthCalledWith(7, 7);
34+
expect(log).toHaveBeenNthCalledWith(8, 8);
35+
expect(log).toHaveBeenNthCalledWith(9, "Fizz");
36+
expect(log).toHaveBeenNthCalledWith(10, "Buzz");
37+
expect(log).toHaveBeenNthCalledWith(11, 11);
38+
expect(log).toHaveBeenNthCalledWith(12, "Fizz");
39+
expect(log).toHaveBeenNthCalledWith(13, 13);
40+
expect(log).toHaveBeenNthCalledWith(14, 14);
41+
expect(log).toHaveBeenNthCalledWith(15, "FizzBuzz");
42+
43+
log.mockRestore();
44+
});

0 commit comments

Comments
 (0)