From 6935c27dbe38da0488afef9aa52e0670289aa07f Mon Sep 17 00:00:00 2001 From: DerekJxy <54869065+DerekJxy@users.noreply.github.com> Date: Tue, 26 Oct 2021 17:35:35 -0400 Subject: [PATCH 1/2] Update README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index ce1f268..dda3395 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,16 @@ let mergedArray = helpful.mergeArrays([1, 2, 3], [2, 3, 4]); // [1, 2, 3, 4] **Return Type:** Array (`Array [1, 2, 3, 4]`) +### average +Find the average of an array +```javascript +let averageOfArray = helpful.average([1, 2, 4, 4]); // 2.75 +``` +**Parameters:** +- array: Array (`[1, 2, 4, 4]`) + +**Return Type:** Number (`2.75`) + ## Hex ### hex.convertFromString From 52b278cff2ac79e1b705f068e6b4750422ddefad Mon Sep 17 00:00:00 2001 From: DerekJxy Date: Wed, 17 Nov 2021 15:00:32 -0500 Subject: [PATCH 2/2] Fix issue #27 --- README.md | 11 ++++++----- helpful.js | 16 ++++++++++++++++ test/tester.js | 7 +++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 77dd178..7ada02a 100644 --- a/README.md +++ b/README.md @@ -390,15 +390,16 @@ let deepFlattenedArray3 = helpful.deepFlattenArray([[0, 1], [2, [3, 4, [5, [6]]] **Return Type:** Array (`Array(5) [0, 1, 2, 3, 4]`) -### average -Find the average of an array +### differenceOfObjects +Finds the difference of two objects (any keys that are present in the second object are removed from the first) ```javascript -let averageOfArray = helpful.average([1, 2, 4, 4]); // 2.75 +let differenceOfObjs = helpful.differenceOfObjects({"a": 1, "b": 2}, {"b": 3, "c": 4}); // {"a": 1} ``` **Parameters:** -- array: Array (`[1, 2, 4, 4]`) +- object1: Object (`{"a": 1, "b": 2}`) +- object2: Object (`{"b": 3, "c": 4}`) -**Return Type:** Number (`2.75`) +**Return Type:** Object (`{"a": 1}`) ## Hex diff --git a/helpful.js b/helpful.js index b17a45a..e47c648 100644 --- a/helpful.js +++ b/helpful.js @@ -235,6 +235,22 @@ return res; } + helpful.differenceOfObjects = function(object1, object2){ + if(Object.keys(object1).length === 0 || Object.keys(object2).length === 0) { + return []; + } + let array1 = Object.keys(object1); + let array2 = Object.keys(object2); + for(let i = 0; i < array1.length; i++){ + for(let j = 0; j < array2.length; j++){ + if(array1[i] == array2[j]){ + delete object1[array1[i]]; + } + } + } + return object1; + } + helpful.hex = {}; /* Modified from https://github.com/TogaTech/tEnvoy */ diff --git a/test/tester.js b/test/tester.js index 3bf5781..e906fc6 100644 --- a/test/tester.js +++ b/test/tester.js @@ -211,6 +211,13 @@ describe("Tests", function() { let actual = helpful.deepFlattenArray([[0, 1], [2, [3, 4, [5, [6]]]]]); assert.deepEqual(expected, actual); }); + + i++; + it(`${i}: differenceOfObjects - Should remove any keys from the first object that are presented in the second object`, function() { + let expected = {"a": 1}; + let actual = helpful.differenceOfObjects({"a": 1, "b": 2}, {"b": 3, "c": 4}); + assert.deepEqual(expected, actual); + }); }); describe("Hex", function() {