Travis build: 1329 [cron]
This commit is contained in:
@ -9,18 +9,19 @@ These snippets, while useful and interesting, didn't quite make it into the repo
|
|||||||
* [`JSONToDate`](#jsontodate)
|
* [`JSONToDate`](#jsontodate)
|
||||||
* [`speechSynthesis`](#speechsynthesis)
|
* [`speechSynthesis`](#speechsynthesis)
|
||||||
* [`binarySearch`](#binarysearch)
|
* [`binarySearch`](#binarysearch)
|
||||||
|
* [`cleanObj`](#cleanobj)
|
||||||
* [`collatz`](#collatz)
|
* [`collatz`](#collatz)
|
||||||
* [`countVowels`](#countvowels)
|
* [`countVowels`](#countvowels)
|
||||||
* [`factors`](#factors)
|
* [`factors`](#factors)
|
||||||
* [`fibonacciCountUntilNum`](#fibonaccicountuntilnum)
|
* [`fibonacciCountUntilNum`](#fibonaccicountuntilnum)
|
||||||
* [`fibonacciUntilNum`](#fibonacciuntilnum)
|
* [`howManyTimes`](#howmanytimes)
|
||||||
* [`httpDelete`](#httpdelete)
|
* [`httpDelete`](#httpdelete)
|
||||||
* [`httpPut`](#httpput)
|
* [`httpPut`](#httpput)
|
||||||
* [`isArmstrongNumber`](#isarmstrongnumber)
|
* [`isArmstrongNumber`](#isarmstrongnumber)
|
||||||
* [`quickSort`](#quicksort)
|
* [`quickSort`](#quicksort)
|
||||||
* [`removeVowels`](#removevowels)
|
* [`removeVowels`](#removevowels)
|
||||||
* [`solveRPN`](#solverpn)
|
* [`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
|
|||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ 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;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Examples</summary>
|
||||||
|
|
||||||
|
```js
|
||||||
|
const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } };
|
||||||
|
cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}}
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
### collatz
|
### collatz
|
||||||
|
|
||||||
Applies the Collatz algorithm.
|
Applies the Collatz algorithm.
|
||||||
@ -230,21 +264,26 @@ fibonacciCountUntilNum(10); // 7
|
|||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ 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`).
|
If `divisor` is `-1` or `1` return `Infinity`.
|
||||||
Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two.
|
If `divisor` is `-0` or `0` return `0`.
|
||||||
Uses a mathematical formula to calculate the length of the array required.
|
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
|
```js
|
||||||
const fibonacciUntilNum = num => {
|
const howManyTimes = (num, divisor) => {
|
||||||
let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
if (divisor === 1 || divisor === -1) return Infinity;
|
||||||
return Array.from({ length: n }).reduce(
|
if (divisor === 0) return 0;
|
||||||
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
|
let i = 0;
|
||||||
[]
|
while (Number.isInteger(num / divisor)) {
|
||||||
);
|
i++;
|
||||||
|
num = num / divisor;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -252,7 +291,10 @@ const fibonacciUntilNum = num => {
|
|||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```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
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
@ -466,26 +508,21 @@ solveRPN('2 3 ^'); // 8
|
|||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ 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.
|
Generates an array, containing the Fibonacci sequence, up until the nth term.
|
||||||
Works for both negative and positive integers.
|
|
||||||
|
|
||||||
If `divisor` is `-1` or `1` return `Infinity`.
|
Create an empty array of the specific length, initializing the first two values (`0` and `1`).
|
||||||
If `divisor` is `-0` or `0` return `0`.
|
Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two.
|
||||||
Otherwise, keep dividing `num` with `divisor` and incrementing `i`, while the result is an integer.
|
Uses a mathematical formula to calculate the length of the array required.
|
||||||
Return the number of times the loop was executed, `i`.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const howManyTimes = (num, divisor) => {
|
const fibonacciUntilNum = num => {
|
||||||
if (divisor === 1 || divisor === -1) return Infinity;
|
let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
||||||
if (divisor === 0) return 0;
|
return Array.from({ length: n }).reduce(
|
||||||
let i = 0;
|
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
|
||||||
while (Number.isInteger(num / divisor)) {
|
[]
|
||||||
i++;
|
);
|
||||||
num = num / divisor;
|
|
||||||
}
|
|
||||||
return i;
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -493,10 +530,7 @@ const howManyTimes = (num, divisor) => {
|
|||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
howManyTimes(100, 2); // 2
|
fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]
|
||||||
howManyTimes(100, 2.5); // 2
|
|
||||||
howManyTimes(100, 0); // 0
|
|
||||||
howManyTimes(100, -1); // Infinity
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|||||||
@ -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
|
> 30-seconds-of-code@0.0.1 test /home/travis/build/Chalarangelo/30-seconds-of-code
|
||||||
> tape test/**/*.test.js | tap-spec
|
> 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
|
total: 563
|
||||||
passing: 563
|
passing: 563
|
||||||
duration: 346ms
|
duration: 391ms
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user