Update howManyTimes.md
This commit is contained in:
@ -1,20 +1,26 @@
|
|||||||
### howManyTimes
|
### howManyTimes
|
||||||
|
|
||||||
Returns the number of times `num` can be divided by `divisor` without getting a fractional answer. Works for both negative and positive integers.`divisor` can be fractional too.
|
Returns the number of times `num` can be divided by `divisor` (integer or fractional) without getting a fractional answer.
|
||||||
If `divisor` is `-1` or `1` returns `Infinity`.
|
Works for both negative and positive integers.
|
||||||
If `divisor` is `-0` or `0` returns `0`.
|
|
||||||
|
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
|
``` js
|
||||||
const howManyTimes = (num,divisor) => {
|
const howManyTimes = (num, divisor) => {
|
||||||
if(divisor === 1 || divisor === -1) return Infinity
|
if(divisor === 1 || divisor === -1) return Infinity;
|
||||||
if(divisor === 0) return 0
|
if(divisor === 0) return 0;
|
||||||
let i = 0
|
let i = 0;
|
||||||
while(Number.isInteger(num/divisor)){
|
while(Number.isInteger(num/divisor)){
|
||||||
i++
|
i++;
|
||||||
num = num / divisor
|
num = num / divisor;
|
||||||
}
|
|
||||||
return i
|
|
||||||
}
|
}
|
||||||
|
return i;
|
||||||
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
howManyTimes(100,2); //2
|
howManyTimes(100,2); //2
|
||||||
howManyTimes(100,-2); //2
|
howManyTimes(100,-2); //2
|
||||||
|
|||||||
Reference in New Issue
Block a user