Repository: pbiswas101/Mathball
Pull request #11 (2019-01-28 09:08:53)

Added isLucky function
**Q**: What version of *Node.js* you've used in the project?
**A**: Node v10.12.0`

**Q**: Mention the *Issue Number*!
**A**: `e.g. #10`

- *Give additional information regarding the PR below:*

------------


codecov-io: # [Codecov](https://codecov.io/gh/pbiswas101/Mathball/pull/11?src=pr&el=h1) Report
> Merging [#11](https://codecov.io/gh/pbiswas101/Mathball/pull/11?src=pr&el=desc) into [master](https://codecov.io/gh/pbiswas101/Mathball/commit/f40edf9ef7e10c4ec330c853befa99473cf0e879?src=pr&el=desc) will **increase** coverage by `0.19%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pbiswas101/Mathball/pull/11/graphs/tree.svg?width=650&token=d91h0D3j17&height=150&src=pr)](https://codecov.io/gh/pbiswas101/Mathball/pull/11?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #11      +/-   ##
==========================================
+ Coverage   96.87%   97.07%   +0.19%     
==========================================
  Files          20       21       +1     
  Lines         192      205      +13     
==========================================
+ Hits          186      199      +13     
  Misses          6        6
```


| [Impacted Files](https://codecov.io/gh/pbiswas101/Mathball/pull/11?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/index.js](https://codecov.io/gh/pbiswas101/Mathball/pull/11/diff?src=pr&el=tree#diff-c3JjL2luZGV4Lmpz) | `100% <100%> (ø)` | :arrow_up: |
| [src/lucky/index.js](https://codecov.io/gh/pbiswas101/Mathball/pull/11/diff?src=pr&el=tree#diff-c3JjL2x1Y2t5L2luZGV4Lmpz) | `100% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pbiswas101/Mathball/pull/11?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pbiswas101/Mathball/pull/11?src=pr&el=footer). Last update [f40edf9...6e9b20e](https://codecov.io/gh/pbiswas101/Mathball/pull/11?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/index.js b/src/index.js
index 1265b7c..84dba62 100644
--- a/src/index.js
+++ b/src/index.js
@@ -9,7 +9,8 @@ const armstrong = require("./armstrong"),
 	prime = require("./prime"),
 	factorial = require("./factorial"),
 	catalan = require("./catalan"),
-	kaprekar = require("./kaprekar");
+	kaprekar = require("./kaprekar"),
+	lucky = require("./lucky");
 
 /* Mathematical Utilities */
 
@@ -36,6 +37,7 @@ module.exports = {
 	isPrime: prime.isPrime,
 	catalan: catalan.catalan,
 	isKaprekar: kaprekar.isKaprekar,
+	isLucky: lucky.isLucky,
 	gcd,
 	isEven,
 	isOdd,
diff --git a/src/lucky/index.js b/src/lucky/index.js
new file mode 100644
index 0000000..093300f
--- /dev/null
+++ b/src/lucky/index.js
@@ -0,0 +1,27 @@
+/* Lucky Number
+ * Function: isLucky()
+ */
+
+function validate(arg) {
+	if (typeof arg != "number" || arg < 1 || !Number.isInteger(arg)) {
+		throw new TypeError(
+			`Invalid argument recieved: ${JSON.stringify(
+				arg
+			)}\n'isLucky()' only accept a positive integer!\n`
+		);
+	}
+}
+
+exports.isLucky = num => {
+	validate(num);
+	var counter = 2;
+	while (true) {
+		if (counter > num) {
+			return true;
+		} else if (num % counter === 0) {
+			return false;
+		}
+		num = num - Math.floor(num / counter);
+		counter++;
+	}
+};


Test patch:
diff --git a/test/lucky-spec.js b/test/lucky-spec.js
new file mode 100644
index 0000000..1bba6a6
--- /dev/null
+++ b/test/lucky-spec.js
@@ -0,0 +1,36 @@
+const assert = require("assert"),
+	isLucky = require("../src/lucky").isLucky;
+
+describe("[Function: isLucky]", () => {
+	it("should be a function", () => {
+		assert.strictEqual(typeof isLucky, "function");
+	});
+
+	it("should return a boolean value when a positive integer is passed", () => {
+		assert.strictEqual(typeof isLucky(1), "boolean");
+	});
+
+	it("should return 'true' when '13' is passed", () => {
+		assert.strictEqual(isLucky(13), true);
+	});
+
+	it("should return 'false' when '2' is passed", () => {
+		assert.strictEqual(isLucky(2), false);
+	});
+
+	it("should throw an error when a negative number is passed", () => {
+		assert.throws(() => isLucky(-20), TypeError);
+	});
+
+	it("should throw an error when a floating point is passed", () => {
+		assert.throws(() => isLucky(31.101996), TypeError);
+	});
+
+	it("should throw an error when a string is passed", () => {
+		assert.throws(() => isLucky("31"), TypeError);
+	});
+
+	it("should throw an error when no arguments passed", () => {
+		assert.throws(() => isLucky(), TypeError);
+	});
+});