Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args);
+ } 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less. Search for snippet...
Adapter call collectInto flip pipeFunctions promisify spreadOver Array chunk compact countOccurrences deepFlatten difference differenceWith distinctValuesOfArray dropElements dropRight everyNth filterNonUnique flatten flattenDepth groupBy head initial initialize2DArray initializeArrayWithRange initializeArrayWithValues intersection isSorted join last mapObject maxN minN nthElement pick pull pullAtIndex pullAtValue quickSort reducedFilter remove sample sampleSize shuffle similarity sortedIndex symmetricDifference tail take takeRight union without zip zipObject Browser arrayToHtmlList bottomVisible copyToClipboard currentURL detectDeviceType elementIsVisibleInViewport getScrollPosition getStyle hasClass hide httpsRedirect onUserInputChange redirect runAsync scrollToTop setStyle show speechSynthesis toggleClass UUIDGeneratorBrowser Date getDaysDiffBetweenDates JSONToDate toEnglishDate tomorrow Function chainAsync compose curry defer functionName memoize once runPromisesInSeries sleep Logic negate Math average clampNumber collatz digitize distance elo factorial factors fibonacci fibonacciCountUntilNum fibonacciUntilNum gcd geometricProgression hammingDistance inRange isArmstrongNumber isDivisible isEven isPrime lcm median percentile powerset primes randomIntegerInRange randomNumberInRange round solveRPN standardDeviation sum sumPower Node hasFlags isTravisCI JSONToFile readFileLines untildify UUIDGeneratorNode Object cleanObj invertKeyValues lowercaseKeys objectFromPairs objectToPairs orderBy select shallowClone size truthCheckCollection String anagrams byteSize capitalize capitalizeEveryWord countVowels escapeHTML escapeRegExp fromCamelCase isAbsoluteURL mask palindrome pluralize repeatString reverseString sortCharactersInString splitLines toCamelCase toKebabCase toSnakeCase truncateString unescapeHTML words Utility cloneRegExp coalesce coalesceFactory extendHex getType getURLParameters hexToRGB isArray isArrayLike isBoolean isFunction isNull isNumber isPrimitive isPromiseLike isString isSymbol isValidJSON prettyBytes randomHexColorCode RGBToHex sdbm timeTaken toDecimalMark toOrdinalSuffix validateNumber yesNo Uncategorized howManyTimes Adapter
call Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args);
Promise.resolve([1, 2, 3])
.then(call('map', x => 2 * x))
.then(console.log); //[ 2, 4, 6 ]
@@ -1202,4 +1202,21 @@ console.log(sdbm('age')); // 808122783
yesNo('yes'); // true
yesNo('No'); // false
yesNo('Foo', true); // true
+Uncategorized
howManyTimes 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.
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.
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;
+};
+howManyTimes(100, 2); //2
+howManyTimes(100, -2); //2
+howManyTimes(100, 2.5); //2
+howManyTimes(100, 3); //0
+howManyTimes(100, 0); //0
+howManyTimes(100, 1); //Infinity
+howManyTimes(100, -1); //Infinity
\ No newline at end of file
diff --git a/snippets/howManyTimes.md b/snippets/howManyTimes.md
index 6b231975b..add649172 100644
--- a/snippets/howManyTimes.md
+++ b/snippets/howManyTimes.md
@@ -8,12 +8,12 @@ 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
+```js
const howManyTimes = (num, divisor) => {
- if(divisor === 1 || divisor === -1) return Infinity;
- if(divisor === 0) return 0;
+ if (divisor === 1 || divisor === -1) return Infinity;
+ if (divisor === 0) return 0;
let i = 0;
- while(Number.isInteger(num/divisor)){
+ while (Number.isInteger(num / divisor)) {
i++;
num = num / divisor;
}
@@ -22,11 +22,11 @@ const howManyTimes = (num, divisor) => {
```
```js
-howManyTimes(100,2); //2
-howManyTimes(100,-2); //2
-howManyTimes(100,2.5); //2
-howManyTimes(100,3); //0
-howManyTimes(100,0); //0
-howManyTimes(100,1); //Infinity
-howManyTimes(100,-1); //Infinity
+howManyTimes(100, 2); //2
+howManyTimes(100, -2); //2
+howManyTimes(100, 2.5); //2
+howManyTimes(100, 3); //0
+howManyTimes(100, 0); //0
+howManyTimes(100, 1); //Infinity
+howManyTimes(100, -1); //Infinity
```
diff --git a/tag_database b/tag_database
index f6e3a8a4b..16dc296a5 100644
--- a/tag_database
+++ b/tag_database
@@ -63,6 +63,7 @@ hasFlags:node
head:array
hexToRGB:utility
hide:browser
+howManyTimes:uncategorized
httpsRedirect:browser
initial:array
initialize2DArray:array