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 c78f6ed

Browse files
Merge pull request knaxus#26 from LeoSL/add-test-to-max-product-of-3-numbers
Add unit tests to Max Product of 3 numbers problem
2 parents 84061cf + 5dca089 commit c78f6ed

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
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 };
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
34+
it("returns an error if there are less than 3 numbers", () => {
35+
expect(() => {
36+
maxProductof3Numbers([-10, -1]);
37+
}).toThrowError();
38+
});
39+
});

0 commit comments

Comments
(0)

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