diff --git a/package-lock.json b/package-lock.json index dd0b763a..021d5ed3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2123,7 +2123,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -2144,12 +2145,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2164,17 +2167,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2291,7 +2297,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2303,6 +2310,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2317,6 +2325,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2324,12 +2333,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2348,6 +2359,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2428,7 +2440,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2440,6 +2453,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2525,7 +2539,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -2561,6 +2576,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2580,6 +2596,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2623,12 +2640,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/src/_Problems_/find-2-nums-adding-to-n/find-2-nums-adding-to-n.test.js b/src/_Problems_/find-2-nums-adding-to-n/find-2-nums-adding-to-n.test.js new file mode 100644 index 00000000..2439b193 --- /dev/null +++ b/src/_Problems_/find-2-nums-adding-to-n/find-2-nums-adding-to-n.test.js @@ -0,0 +1,34 @@ +const { findTwoNumsAddingToN, findTwoNumsAddingToN2 } = require('.'); + +describe('Find two numbers adding to N', () => { + [findTwoNumsAddingToN, findTwoNumsAddingToN2].forEach((func) => { + describe(func.name, () => { + it('Should return an array with length two', () => { + expect(findTwoNumsAddingToN2([1, 2], 3).length).toBe(2); + }); + + it('Should return false when there is no solution', () => { + expect(findTwoNumsAddingToN2([1, 2], 5)).toBe(false); + }); + + it('Should return false input array length is less than 2', () => { + expect(findTwoNumsAddingToN2([5], 5)).toBe(false); + }); + + it('Should return negative values', () => { + expect(findTwoNumsAddingToN2([-2, 1, 2, 6, 7], 5)).toEqual(expect.arrayContaining([-2, 7])); + }); + + it('Should return two numbers that sum to N', () => { + expect(findTwoNumsAddingToN([1, 2, 3, 5], 5)).toEqual(expect.arrayContaining([2, 3])); + }); + }); + }); + + describe('function differences findTwoNumsAddingToN and findTwoNumsAddingToN2', () => { + it('Should return different arrays', () => { + expect(findTwoNumsAddingToN([1, 2, 3, 4], 5)) + .toEqual(expect.not.arrayContaining(findTwoNumsAddingToN2([1, 2, 3, 4], 5))); + }); + }); +}); diff --git a/src/_Problems_/find-2-nums-adding-to-n/index.js b/src/_Problems_/find-2-nums-adding-to-n/index.js index 5ba3e0a7..74416cd7 100644 --- a/src/_Problems_/find-2-nums-adding-to-n/index.js +++ b/src/_Problems_/find-2-nums-adding-to-n/index.js @@ -18,15 +18,19 @@ function findTwoNumsAddingToN(arr, number) { // the Brute force approach function findTwoNumsAddingToN2(arr, number) { - const pair = []; + for (let i = 0; i < arr.length; i += 1) { for (let j = i + 1; j < arr.length; j += 1) { if (arr[i] + arr[j] === number) { - pair.push(arr[i], arr[j]); - break; + return [arr[i], arr[j]]; } } } - return pair.length ? pair : false; + return false; } + +module.exports = { + findTwoNumsAddingToN, + findTwoNumsAddingToN2, +};