Skip to content

Commit e47609d

Browse files
committed
Add unit tests to Max Product of 3 numbers problem
1 parent e7f1166 commit e47609d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/_Problems_/max-product-of-3-numbers/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ function maxProductof3NumbersII(arr) {
6363

6464
return p1 > p2 ? p1 : p2;
6565
}
66+
67+
module.exports = { maxProductof3Numbers, maxProductof3NumbersII };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { maxProductof3Numbers, maxProductof3NumbersII } = require(".");
2+
3+
describe("Maximum Product of three numbers", () => {
4+
it("throws an error with no Array is passed", () => {
5+
expect(() => {
6+
maxProductof3Numbers("xunda");
7+
}).toThrowError();
8+
expect(() => {
9+
maxProductof3NumbersII("xunda");
10+
}).toThrowError();
11+
});
12+
13+
it("returns the product of an array with 3 numbers", () => {
14+
expect(maxProductof3Numbers([1, 2, 3])).toEqual(6);
15+
expect(maxProductof3NumbersII([1, 2, 3])).toEqual(6);
16+
});
17+
18+
it("returns the product of an array with positive and negative numbers", () => {
19+
expect(maxProductof3Numbers([-10, -10, 2, 3])).toEqual(300);
20+
expect(maxProductof3NumbersII([-10, -10, 2, 3])).toEqual(300);
21+
});
22+
23+
it("returns the product of an array with negative numbers", () => {
24+
expect(maxProductof3Numbers([-10, -1, -2, -10])).toEqual(-20);
25+
expect(maxProductof3NumbersII([-10, -1, -2, -10])).toEqual(-20);
26+
});
27+
28+
it("returns the proper calculation if the array is large", () => {
29+
const largeArray = [100, 100, 100, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 45, 4, 3, 7, 8, 1, 3, 7, 8, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 45, 4, 3, 7, 8, 1, 3, 7, 8];
30+
expect(maxProductof3Numbers(largeArray)).toEqual(100 * 100 * 100);
31+
expect(maxProductof3NumbersII(largeArray)).toEqual(100 * 100 * 100);
32+
});
33+
});

0 commit comments

Comments
 (0)