From 9b28bacb5e839c0f15efceded40e18efaffe720d Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 18:45:13 +0530 Subject: [PATCH 1/3] Create howManyTimes.md --- snippets/howManyTimes.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 snippets/howManyTimes.md diff --git a/snippets/howManyTimes.md b/snippets/howManyTimes.md new file mode 100644 index 000000000..16e048936 --- /dev/null +++ b/snippets/howManyTimes.md @@ -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 +``` From edcb375377d6fc8fb49e533a9ac27f2af1543786 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 18:46:16 +0530 Subject: [PATCH 2/3] Update howManyTimes.md --- snippets/howManyTimes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/howManyTimes.md b/snippets/howManyTimes.md index 16e048936..3d2cfa91d 100644 --- a/snippets/howManyTimes.md +++ b/snippets/howManyTimes.md @@ -1,6 +1,6 @@ ### 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. +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. If `divisor` is `-1` or `1` returns `Infinity`. If `divisor` is `-0` or `0` returns `0`. ``` js From bc6cdcd2ecf234b03bd8448cf1a4002197a52fa4 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 3 Jan 2018 16:02:31 +0200 Subject: [PATCH 3/3] Update howManyTimes.md --- snippets/howManyTimes.md | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/snippets/howManyTimes.md b/snippets/howManyTimes.md index 3d2cfa91d..6b231975b 100644 --- a/snippets/howManyTimes.md +++ b/snippets/howManyTimes.md @@ -1,20 +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 fractional too. -If `divisor` is `-1` or `1` returns `Infinity`. -If `divisor` is `-0` or `0` returns `0`. +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 +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