Repository: pbiswas101/Mathball
Pull request #9 (2019-01-27 19:52:56)

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


codecov-io: # [Codecov](https://codecov.io/gh/pbiswas101/Mathball/pull/9?src=pr&el=h1) Report
> Merging [#9](https://codecov.io/gh/pbiswas101/Mathball/pull/9?src=pr&el=desc) into [master](https://codecov.io/gh/pbiswas101/Mathball/commit/89000fc5ed87c8798d0d22d276640fcac5149ea7?src=pr&el=desc) will **decrease** coverage by `0.23%`.
> The diff coverage is `95.91%`.

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

```diff
@@            Coverage Diff             @@
##           master       #9      +/-   ##
==========================================
- Coverage   97.09%   96.85%   -0.24%     
==========================================
  Files          19       20       +1     
  Lines         172      191      +19     
==========================================
+ Hits          167      185      +18     
- Misses          5        6       +1
```


| [Impacted Files](https://codecov.io/gh/pbiswas101/Mathball/pull/9?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/index.js](https://codecov.io/gh/pbiswas101/Mathball/pull/9/diff?src=pr&el=tree#diff-c3JjL2luZGV4Lmpz) | `100% <100%> (ø)` | :arrow_up: |
| [src/catalan/index.js](https://codecov.io/gh/pbiswas101/Mathball/pull/9/diff?src=pr&el=tree#diff-c3JjL2NhdGFsYW4vaW5kZXguanM=) | `92.3% <92.3%> (ø)` | :arrow_up: |
| [src/kaprekar/index.js](https://codecov.io/gh/pbiswas101/Mathball/pull/9/diff?src=pr&el=tree#diff-c3JjL2thcHJla2FyL2luZGV4Lmpz) | `94.44% <94.44%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pbiswas101/Mathball/pull/9?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/9?src=pr&el=footer). Last update [89000fc...2f8cdc6](https://codecov.io/gh/pbiswas101/Mathball/pull/9?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 60503ac..1265b7c 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,25 +1,27 @@
 /* Types of Numbers */
 
-const armstrong = require('./armstrong'),
-	automorphic = require('./automorphic'),
-	fibonacci = require('./fibonacci'),
-	magic = require('./magic'),
-	neon = require('./neon'),
-	palindrome = require('./palindrome'),
-	prime = require('./prime'),
-	factorial = require('./factorial');
+const armstrong = require("./armstrong"),
+	automorphic = require("./automorphic"),
+	fibonacci = require("./fibonacci"),
+	magic = require("./magic"),
+	neon = require("./neon"),
+	palindrome = require("./palindrome"),
+	prime = require("./prime"),
+	factorial = require("./factorial"),
+	catalan = require("./catalan"),
+	kaprekar = require("./kaprekar");
 
 /* Mathematical Utilities */
 
-const gcd = require('./gcd'),
-	isEven = require('./isEven'),
-	isOdd = require('./isOdd'),
-	lcm = require('./lcm'),
-	length = require('./length'),
-	max = require('./max'),
-	min = require('./min'),
-	sort = require('./sort'),
-	sum = require('./sum');
+const gcd = require("./gcd"),
+	isEven = require("./isEven"),
+	isOdd = require("./isOdd"),
+	lcm = require("./lcm"),
+	length = require("./length"),
+	max = require("./max"),
+	min = require("./min"),
+	sort = require("./sort"),
+	sum = require("./sum");
 
 /* M - mathball utility object */
 
@@ -32,6 +34,8 @@ module.exports = {
 	isNeon: neon.isNeon,
 	isPalindrome: palindrome.isPalindrome,
 	isPrime: prime.isPrime,
+	catalan: catalan.catalan,
+	isKaprekar: kaprekar.isKaprekar,
 	gcd,
 	isEven,
 	isOdd,
diff --git a/src/kaprekar/index.js b/src/kaprekar/index.js
new file mode 100644
index 0000000..6b63263
--- /dev/null
+++ b/src/kaprekar/index.js
@@ -0,0 +1,35 @@
+/* Kaprekar Number
+ * Function: isKaprekar()
+ */
+
+function validate(arg) {
+	if (typeof arg != "number" || arg < 1 || !Number.isInteger(arg)) {
+		throw new TypeError(
+			`Invalid argument recieved: ${JSON.stringify(
+				arg
+			)}\n'isKaprekar()' only accepts a positive integer!\n`
+		);
+	}
+}
+
+exports.isKaprekar = num => {
+	validate(num);
+	if (num == 1) {
+		return true;
+	}
+	var sqNum = num * num;
+	var countDigits = num.toString().length;
+	var rDigits = 0;
+	while (rDigits < countDigits) {
+		rDigits++;
+		var eqParts = Math.pow(10, rDigits);
+		if (eqParts == num) {
+			continue;
+		}
+		var sum = Math.floor(sqNum / eqParts) + (sqNum % eqParts);
+		if (sum == num) {
+			return true;
+		}
+	}
+	return false;
+};


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