diff --git a/snippets_archive/README.md b/snippets_archive/README.md index e51a9be23..bd8901939 100644 --- a/snippets_archive/README.md +++ b/snippets_archive/README.md @@ -9,18 +9,19 @@ These snippets, while useful and interesting, didn't quite make it into the repo * [`JSONToDate`](#jsontodate) * [`speechSynthesis`](#speechsynthesis) * [`binarySearch`](#binarysearch) +* [`cleanObj`](#cleanobj) * [`collatz`](#collatz) * [`countVowels`](#countvowels) * [`factors`](#factors) * [`fibonacciCountUntilNum`](#fibonaccicountuntilnum) -* [`fibonacciUntilNum`](#fibonacciuntilnum) +* [`howManyTimes`](#howmanytimes) * [`httpDelete`](#httpdelete) * [`httpPut`](#httpput) * [`isArmstrongNumber`](#isarmstrongnumber) * [`quickSort`](#quicksort) * [`removeVowels`](#removevowels) * [`solveRPN`](#solverpn) -* [`howManyTimes`](#howmanytimes) +* [`fibonacciUntilNum`](#fibonacciuntilnum) --- @@ -111,6 +112,39 @@ binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 21); // -1
[⬆ Back to top](#table-of-contents) +### cleanObj + +Removes any properties except the ones specified from a JSON object. + +Use `Object.keys()` method to loop over given JSON object and deleting keys that are not included in given array. +If you pass a special key,`childIndicator`, it will search deeply apply the function to inner objects, too. + +```js +const cleanObj = (obj, keysToKeep = [], childIndicator) => { + Object.keys(obj).forEach(key => { + if (key === childIndicator) { + cleanObj(obj[key], keysToKeep, childIndicator); + } else if (!keysToKeep.includes(key)) { + delete obj[key]; + } + }); + return obj; +}; +``` + +
+Examples + +```js +const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } }; +cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}} +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### collatz Applies the Collatz algorithm. @@ -230,21 +264,26 @@ fibonacciCountUntilNum(10); // 7
[⬆ Back to top](#table-of-contents) -### fibonacciUntilNum +### howManyTimes -Generates an array, containing the Fibonacci sequence, up until the nth term. +Returns the number of times `num` can be divided by `divisor` (integer or fractional) without getting a fractional answer. +Works for both negative and positive integers. -Create an empty array of the specific length, initializing the first two values (`0` and `1`). -Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two. -Uses a mathematical formula to calculate the length of the array required. +If `divisor` is `-1` or `1` return `Infinity`. +If `divisor` is `-0` or `0` return `0`. +Otherwise, keep dividing `num` with `divisor` and incrementing `i`, while the result is an integer. +Return the number of times the loop was executed, `i`. ```js -const fibonacciUntilNum = num => { - let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2)); - return Array.from({ length: n }).reduce( - (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), - [] - ); +const howManyTimes = (num, divisor) => { + if (divisor === 1 || divisor === -1) return Infinity; + if (divisor === 0) return 0; + let i = 0; + while (Number.isInteger(num / divisor)) { + i++; + num = num / divisor; + } + return i; }; ``` @@ -252,7 +291,10 @@ const fibonacciUntilNum = num => { Examples ```js -fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ] +howManyTimes(100, 2); // 2 +howManyTimes(100, 2.5); // 2 +howManyTimes(100, 0); // 0 +howManyTimes(100, -1); // Infinity ``` @@ -466,26 +508,21 @@ solveRPN('2 3 ^'); // 8
[⬆ Back to top](#table-of-contents) -### howManyTimes +### fibonacciUntilNum -Returns the number of times `num` can be divided by `divisor` (integer or fractional) without getting a fractional answer. -Works for both negative and positive integers. +Generates an array, containing the Fibonacci sequence, up until the nth term. -If `divisor` is `-1` or `1` return `Infinity`. -If `divisor` is `-0` or `0` return `0`. -Otherwise, keep dividing `num` with `divisor` and incrementing `i`, while the result is an integer. -Return the number of times the loop was executed, `i`. +Create an empty array of the specific length, initializing the first two values (`0` and `1`). +Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two. +Uses a mathematical formula to calculate the length of the array required. ```js -const howManyTimes = (num, divisor) => { - if (divisor === 1 || divisor === -1) return Infinity; - if (divisor === 0) return 0; - let i = 0; - while (Number.isInteger(num / divisor)) { - i++; - num = num / divisor; - } - return i; +const fibonacciUntilNum = num => { + let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2)); + return Array.from({ length: n }).reduce( + (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), + [] + ); }; ``` @@ -493,10 +530,7 @@ const howManyTimes = (num, divisor) => { Examples ```js -howManyTimes(100, 2); // 2 -howManyTimes(100, 2.5); // 2 -howManyTimes(100, 0); // 0 -howManyTimes(100, -1); // Infinity +fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ] ``` diff --git a/test/testlog b/test/testlog index 73abf076c..32994aa4e 100644 --- a/test/testlog +++ b/test/testlog @@ -1,4 +1,4 @@ -Test log for: Sat Jan 20 2018 20:10:48 GMT+0000 (UTC) +Test log for: Sun Jan 21 2018 20:11:24 GMT+0000 (UTC) > 30-seconds-of-code@0.0.1 test /home/travis/build/Chalarangelo/30-seconds-of-code > tape test/**/*.test.js | tap-spec @@ -1299,6 +1299,6 @@ Test log for: Sat Jan 20 2018 20:10:48 GMT+0000 (UTC) total: 563 passing: 563 - duration: 346ms + duration: 391ms