Create howManyTimes.md

This commit is contained in:
Rohit Tanwar
2018-01-03 18:45:13 +05:30
committed by GitHub
parent b013903e6c
commit 250258a925

26
snippets/howManyTimes.md Normal file
View File

@ -0,0 +1,26 @@
### 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 positive too.
If `divisor` is `-1` or `1` returns `Infinity`.
If `divisor` is `-0` or `0` returns `0`.
``` 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
```