diff --git a/README.md b/README.md index 05c69c1c9..36d542f47 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,15 @@ +### _Uncategorized_ + +
+View contents + +* [`howManyTimes`](#howmanytimes) + +
+ --- ## 🔌 Adapter @@ -5255,6 +5264,44 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) +--- + ## _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`. + +```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; +}; +``` + +```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 +``` + +
[⬆ back to top](#table-of-contents) + ## Collaborators diff --git a/docs/index.html b/docs/index.html index 199e601e5..d0361c2ab 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

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);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

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