From a619a2985d6b1ef1d0340c2d0035dc255b5b8799 Mon Sep 17 00:00:00 2001 From: Emmanuel Agbavwe Date: Thu, 28 Jul 2022 23:44:41 +0100 Subject: [PATCH] Update README.md, AroTable.js Added explanatory comments for the private methods used in the AroTable class --- AroTable.js | 30 ++++++++++++++++++++++-------- README.md | 14 +++++++------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/AroTable.js b/AroTable.js index 3758bf2..857030f 100644 --- a/AroTable.js +++ b/AroTable.js @@ -35,6 +35,7 @@ export default class AroTable { this.add(number, ...numbers); } + // A utility method that implements the MergeSort sorting algorithm #mergeSort (array) { if (array.length <= 1) return array; @@ -62,6 +63,7 @@ export default class AroTable { return sorted_values; }; + // A utility method that implements aggressive approximation to 3 decimal places #trimNum (el) { let strDp = String(+(Math.round(el % 1 + 'e+3') + 'e-3')); el > 0 ? @@ -84,15 +86,18 @@ export default class AroTable { return el < 0 && (el - (el % 1)) == 0 ? Number(str) * -1 : Number(str); } + // A utility method that splits the number argument into the whole number part and its decimal part rounded up to 3 decimal places #returnParts (el) { const dp = this.#trimNum(el % 1); return dp == 1 ? [this.#trimNum((el - (el % 1)) + (el > 0 ? dp : dp * -1)), 0] : [this.#trimNum((el) - (el % 1)), dp]; } + // A utility method used to split the user's number input into its whole number part and its exact decimal part untouched, with no approximation applied #returnInputParts (number) { return [number - (number % 1), number % 1 != 0 ? Number(`${number < 0 ? '-0' : '0'}.` + String(number).split('.')[1]) : (number % 1)]; } + // A utility method that arranges numbers into the array representation with the values in the AroTable that have a valid occurrence. At the same it manages the logging of the initial occurrence and occurrence count of individual numbers in the AroTable in the array to be returned upon a search for a number with a valid occurrence #arrange () { let index = 0; this.#array = []; @@ -147,10 +152,10 @@ export default class AroTable { this.#pos[numValue][0] = index; if (this.#pos[numValue][2] > 1) { - const keys = this.#mergeSort(Object.keys(this.#pos[numValue][1])); + const keys = this.#mergeSort(Object.keys(this.#pos[numValue][1])), keysLength = keys.length; let x = 0; - for (x; x < keys.length; x++) { + for (x; x < keysLength; x++) { const oc = this.#pos[numValue][1][String(keys[x])][1]; this.#pos[numValue][1][String(keys[x])][0] = index; let i = 0; @@ -178,6 +183,7 @@ export default class AroTable { } }; + // A utility method that inserts a single number into the AroTable #insert (number) { if (!number && number !== 0 || isNaN(number) || @@ -205,13 +211,15 @@ export default class AroTable { return; } + // A utility method that inserts numbers from an array into the AroTable #insertArray (numbers) { if (numbers == null || numbers == undefined || !numbers.length || !Array.isArray(numbers)) return false; + const numLength = numbers.length; let index = 0; - for (index; index < numbers.length; index++) { + for (index; index < numLength; index++) { let element = numbers[index]; Array.isArray(element) ? this.#insertArray(element) : @@ -220,15 +228,18 @@ export default class AroTable { return; } + // A utility method that implements the removal of a single occurrence of a number from the AroTable #enforceRemove (number = null, ...numbers) { if (numbers) { + const numLength = numbers.length; let i = 0; - for (i; i < numbers.length; i++) + for (i; i < numLength; i++) this.#enforceRemove(numbers[i]); } if (Array.isArray(number)) { + const numLength = numbers.length; let i = 0; - for (i; i < number.length; i++) + for (i; i < numLength; i++) this.#enforceRemove(number[i]); } else if (this.search(number)) { @@ -246,15 +257,18 @@ export default class AroTable { return; } + // A utility method that implements the complete removal of all occurrences of a number from the AroTable #enforceRemoveAll (number = null, ...numbers) { if (numbers) { + const numLength = numbers.length; let i = 0; - for (i; i < numbers.length; i++) + for (i; i < numLength; i++) this.#enforceRemoveAll(numbers[i]); } if (Array.isArray(number)) { + const numLength = numbers.length; let i = 0; - for (i; i < number.length; i++) + for (i; i < numLength; i++) this.#enforceRemoveAll(number[i]); } else if (this.search(number)) { @@ -391,7 +405,7 @@ export default class AroTable { /** * Returns any value in the AroTable that meets the condition specified in a callback function. * @param {Function} qualifier A function that takes the desired value to be evaluated. The returnAny method calls the qualifier function once for each number in the AroTable. - * @returns True if at least one matching value is found, returns false if not. + * @returns An array containing the number(s) that meet the condition, returns false if none could be found that fulfills the condition. */ returnAny (qualifier) { const returnArray = []; diff --git a/README.md b/README.md index 35d6f0a..49b89be 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,7 @@ aroTable.add('four', 'five'); // Returns false If the arguments passed contains number convertible types along with non-number convertible types, the **add()** method will add the valid input to the AroTable, ignoring the non-number convertible types: ```js -aroTable.add(1,'-2.1', 'three', -4, '5', null, 7.32, undefined, 'nine'); // returns true +aroTable.add(1,'-2.1', 'three', -4, '5', null, 7.32, undefined, 'nine'); // Returns true // In this case, 1, '-2.1', -4, '5', 7.32 are added to the AroTable, while all other non-number convertible typed values are ignored. ``` @@ -195,7 +195,7 @@ aroTable.returnArray(); // Returns [ 2.7, 5, 6.124, 9.993 ] ### The **returnAny()** Method -The **returnAny()** method, is a higher-order method that takes in a callback function and returns any value in the AroTable that meets the condition specified in the callback function. Returns true if at least a value meets the condition, returns false if not: +The **returnAny()** method, is a higher-order method that takes in a callback function and returns any value in the AroTable that meets the condition specified in the callback function. Returns an array containing the number(s) that meet the condition, returns false if none could be found that fulfills the condition: ```js const aroTable = new AroTable(2.7, 1.2, -2.4, 4, 5, 6.124, 8, -2, 9.993, 1, 0); @@ -234,7 +234,7 @@ aroTable.returnArray(); // Returns [ 1, 2.343, 4, 5.1, 6, 6.3 ] ### The **returnDuplicates()** Method -The **returnDuplicates()** method returns a sorted array (using Merge Sort) of all numbers with duplicated occurrences in the AroTable, if none exists, returns false: +The **returnDuplicates()** method returns a sorted array of all numbers with duplicated occurrences in the AroTable, if none exists, returns false: ```js const aroTable = new AroTable(-1, -2.343, 3, 4, -2.343, 3, 4, -5.1, 6, 6.3, 6.3, 3); @@ -270,7 +270,7 @@ aroTable.dropUnits(); // Returns false ### The **returnUnits()** Method -The **returnUnits()** method returns a sorted array (using Merge Sort) of all numbers with a single occurrence in the AroTable, if none exists, returns false: +The **returnUnits()** method returns a sorted array of all numbers with a single occurrence in the AroTable, if none exists, returns false: ```js const aroTable = new AroTable(-1, -2.343, 3, 4, -2.343, 3, 4, -5.1, 6, 6.3, 6.3, 3); @@ -294,7 +294,7 @@ aroTable.dropPositives(); // Returns false ### The **returnPositives()** Method -The **returnPositives()** method returns a sorted array (using Merge Sort) of all positive numbers in the AroTable, if none exists returns false: +The **returnPositives()** method returns a sorted array of all positive numbers in the AroTable, if none exists returns false: ```js const aroTable = new AroTable(-1, -2.343, 3, 4, -2.343, 3, 4, -5.1, 6, 6.3, 6.3, 3); @@ -318,7 +318,7 @@ aroTable.dropNegatives(); // Returns false ### The **returnNegatives()** Method -The **returnNegatives()** method returns a sorted array (using Merge Sort) of all negative numbers in the AroTable, if none exists returns false: +The **returnNegatives()** method returns a sorted array of all negative numbers in the AroTable, if none exists returns false: ```js const aroTable = new AroTable(-1, -2.343, 3, 4, -2.343, 3, 4, -5.1, 6, 6.3, 6.3, 3); @@ -350,7 +350,7 @@ aroTable.returnArray(); // Returns [] aroTable.getDistribution(); // Returns { 'Positive Numbers': 0, 'Negative Numbers': 0 } ``` -A better way to check if the AroTable is empty, is to use... +> A better way to check if the AroTable is empty, is to use... ### The **isEmpty()** Method