diff --git a/README.md b/README.md index 36d542f47..1ba8cd30e 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,7 @@ * [`gcd`](#gcd) * [`geometricProgression`](#geometricprogression) * [`hammingDistance`](#hammingdistance) +* [`howManyTimes`](#howmanytimes) * [`inRange`](#inrange) * [`isArmstrongNumber`](#isarmstrongnumber) * [`isDivisible`](#isdivisible) @@ -288,15 +289,6 @@ -### _Uncategorized_ - -
-View contents - -* [`howManyTimes`](#howmanytimes) - -
- --- ## 🔌 Adapter @@ -3030,6 +3022,47 @@ hammingDistance(2, 3); // 1
[⬆ Back to top](#table-of-contents) +### 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; +}; +``` + +
+Examples + +```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) + + ### inRange Checks if the given number falls within the given range. @@ -5264,44 +5297,6 @@ 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 d0361c2ab..cd1710c9c 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 ]
@@ -667,6 +667,23 @@ geometricProgression(256, 1, 4); //[1, 4, 16, 64, 256]
 geometricProgression(256, 2, 1); //Gives error
 

hammingDistance

Calculates the Hamming distance between two values.

Use XOR operator (^) to find the bit difference between the two numbers, convert to a binary string using toString(2). Count and return the number of 1s in the string, using match(/1/g).

const hammingDistance = (num1, num2) => ((num1 ^ num2).toString(2).match(/1/g) || '').length;
 
hammingDistance(2, 3); // 1
+

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
 

inRange

Checks if the given number falls within the given range.

Use arithmetic comparison to check if the given number is in the specified range. If the second parameter, end, is not specified, the range is considered to be from 0 to start.

const inRange = (n, start, end = null) => {
   if (end && start > end) end = [start, (start = end)][0];
   return end == null ? n >= 0 && n < start : n >= start && n < end;
@@ -1202,21 +1219,4 @@ 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