From 2bdd4f28a78fb72d45f7a093d7b4ea701e8bd70b Mon Sep 17 00:00:00 2001 From: jgchoti Date: Thu, 25 Apr 2024 14:57:04 +0200 Subject: [PATCH 1/4] Create intersection.js --- src/intersection/intersection.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/intersection/intersection.js diff --git a/src/intersection/intersection.js b/src/intersection/intersection.js new file mode 100644 index 0000000..bb61a2b --- /dev/null +++ b/src/intersection/intersection.js @@ -0,0 +1,23 @@ +/** + * Creates an array of values that are in both the first and the second arrays. + * + * Repeated values are not duplicated in the return value, and the order of result values are determined by the first array. + * + * **Note:** This function returns a new array, and has no side-effects. + * + * @param {Array} [array=[]] - The array to inspect. + * @param {Array} [values=[]] - The values to include. + * @returns {Array} Returns the new array of filtered values. + * @example + * + * intersection([2, 1], [2, 3]); // -> [2] + * + * @example + * + * intersection([2, 1, 2], [2, 3]); // -> [2] + */ +export const intersection = (array = [], values = []) => { + let result = array.filter(x => values.includes(x)); + let noDuplicateResult = new Set(result); + return [...noDuplicateResult] +}; From 40342c25aa30bc0654c19378d52fb3671b743572 Mon Sep 17 00:00:00 2001 From: jgchoti Date: Thu, 25 Apr 2024 14:57:08 +0200 Subject: [PATCH 2/4] Create intersection.spec.js --- src/intersection/intersection.spec.js | 140 ++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/intersection/intersection.spec.js diff --git a/src/intersection/intersection.spec.js b/src/intersection/intersection.spec.js new file mode 100644 index 0000000..eb660d6 --- /dev/null +++ b/src/intersection/intersection.spec.js @@ -0,0 +1,140 @@ +import { difference } from './intersection.js'; + + +// test cases +const cases = [ + { + label: '[2, 1], [2, 3] => [2]', + argA: [2, 1], + argB: [2, 3], + answer: [2] + }, + { + label: '[2, 1, 2], [2, 3] => [2]', + argA: [2, 1, 2], + argB: [2, 3], + answer: [2] + }, + { + label: '[], [1, 2, 3] => []', + argA: [], + argB: [1, 2, 3], + answer: [] + }, + { + label: '[4, 5, 6], [6, 7, 8] => [6]', + argA: [4, 5, 6], + argB: [6, 7, 8], + answer: [6] + }, + { + label: '[10, 20, 30], [30, 40, 50] => [30]', + argA: [10, 20, 30], + argB: [30, 40, 50], + answer: [30] + }, + { + label: '[0.5, 1.5, 2.5], [1.5, 2.5, 3.5] => [1.5, 2.5]', + argA: [0.5, 1.5, 2.5], + argB: [1.5, 2.5, 3.5], + answer: [1.5, 2.5] + }, + { + label: '[0.1, 0.2, 0.3], [0.2, 0.3, 0.4] => [0.2, 0.3]', + argA: [0.1, 0.2, 0.3], + argB: [0.2, 0.3, 0.4], + answer: [0.2, 0.3] + }, + { + label: '[-1, -2, -3], [-2, -3, -4] => [-2, -3]', + argA: [-1, -2, -3], + argB: [-2, -3, -4], + answer: [-2, -3] + }, + { + label: '[1000000000, 2000000000, 3000000000], [2000000000, 3000000000, 4000000000] => [2000000000, 3000000000]', + argA: [1000000000, 2000000000, 3000000000], + argB: [2000000000, 3000000000, 4000000000], + answer: [2000000000, 3000000000] + }, { + label: '[0, 0, 0], [0, 1, 2] => [0]', + argA: [0, 0, 0], + argB: [0, 1, 2], + answer: [0] + }, + { + label: '[-1, -2, -3], [-2, -4, -6] => [-2]', + argA: [-1, -2, -3], + argB: [-2, -4, -6], + answer: [-2] + }, + { + label: '[1, 2, 3], [3, 2, 1] => [1, 2, 3]', + argA: [1, 2, 3], + argB: [3, 2, 1], + answer: [1, 2, 3] + }, { + label: '[1, "2", true], ["2", false] => ["2"]', + argA: [1, '2', true], + argB: ['2', false], + answer: ['2'] + }, + { + label: '[0.0001, 0.0002, 0.0003], [0.0002, 0.0003, 0.0004] => [0.0002, 0.0003]', + argA: [0.0001, 0.0002, 0.0003], + argB: [0.0002, 0.0003, 0.0004], + answer: [0.0002, 0.0003] + }, + { + label: '[1.111, 2.222, 3.333], [2.222, 3.333, 4.444] => [2.222, 3.333]', + argA: [1.111, 2.222, 3.333], + argB: [2.222, 3.333, 4.444], + answer: [2.222, 3.333] + }, + { + label: '[-1000, -2000, -3000], [-2000, -3000, -4000] => [-2000, -3000]', + argA: [-1000, -2000, -3000], + argB: [-2000, -3000, -4000], + answer: [-2000, -3000] + }, + { + label: '[999999, 9999999, 99999999], [9999999, 99999999, 999999999] => [9999999, 99999999]', + argA: [999999, 9999999, 99999999], + argB: [9999999, 99999999, 999999999], + answer: [9999999, 99999999] + }, + { + label: '[6, 6, 3, "hi"], [2, "hi", 46] => ["hi"]', + argA: [6, 6, 3, "hi"], + argB: [2, "hi", 46], + answer: ["hi"] + }, + { + label: '[1.5e6, 2.5e6, 3.5e6], [2.5e6, 3.5e6, 4.5e6] => [2.5e6, 3.5e6]', + argA: [1.5e6, 2.5e6, 3.5e6], + argB: [2.5e6, 3.5e6, 4.5e6], + answer: [2.5e6, 3.5e6] + }, + { + label: "['hi', 'hello', ''], ['good', 'bye', ''] => ['']", + argA: ['hi', 'hello', ''], + argB: ['good', 'bye', ''], + answer: [''] + } +] + + +// run solution to test +for (const solution of [intersection]) { + describe( + solution.name + + ': return an array of values that are in both the first and the second arrays', + () => { + for (const caseInfo of cases) { + it(caseInfo.label, () => { + expect(solution(caseInfo.argA, caseInfo.argB)).toEqual(caseInfo.answer); + }); + } + }, + ); +} From 739c93ac587006c748a06547556ce692465e1fd5 Mon Sep 17 00:00:00 2001 From: jgchoti Date: Thu, 25 Apr 2024 15:02:58 +0200 Subject: [PATCH 3/4] rename function --- src/intersection/intersection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/intersection/intersection.js b/src/intersection/intersection.js index bb61a2b..99d4883 100644 --- a/src/intersection/intersection.js +++ b/src/intersection/intersection.js @@ -16,7 +16,7 @@ * * intersection([2, 1, 2], [2, 3]); // -> [2] */ -export const intersection = (array = [], values = []) => { +export const filterIntersection = (array = [], values = []) => { let result = array.filter(x => values.includes(x)); let noDuplicateResult = new Set(result); return [...noDuplicateResult] From 1fe876b10791648c631b81e0fa696268be6df5ff Mon Sep 17 00:00:00 2001 From: jgchoti Date: Thu, 25 Apr 2024 15:03:03 +0200 Subject: [PATCH 4/4] rename function --- src/intersection/intersection.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/intersection/intersection.spec.js b/src/intersection/intersection.spec.js index eb660d6..a91df2f 100644 --- a/src/intersection/intersection.spec.js +++ b/src/intersection/intersection.spec.js @@ -1,4 +1,4 @@ -import { difference } from './intersection.js'; +import { filterIntersection } from './intersection.js'; // test cases @@ -125,7 +125,7 @@ const cases = [ // run solution to test -for (const solution of [intersection]) { +for (const solution of [filterIntersection]) { describe( solution.name + ': return an array of values that are in both the first and the second arrays',