Repository: pbiswas101/Mathball (js)

Issue:
Kaprekar Number
### Do the checklist before filing the issue:

- [ ] Is this something you can **debug and fix**?
- [x] Do you have Node.js and all the relevant dev-dependencies installed?
- [ ] Is this a **feature request** that you can add?

###### NOTE: Provide a clear and concise description of the feature that needs to be added! Or if its a bug, then provide the necessary steps to reproduce it.

- *Give your answer below:*

------------

[Function: **M.isKaprekar()**] needs to be added!
For more info:

https://www.geeksforgeeks.org/kaprekar-number/

*M.isKaprekar()* needs to accept a positive integer and all other arguments passed should throw a type error which needs to be handled gracefully! For error handling, you should refer to how other features like M.isArmstrong() is implemented.

Pull request description:
Adding matrix multplication feature
### Do the checklist before submitting the PR:

- [x] Have you read the guidelines mentioned in **CONTRIBUTING.md**?
- [x] Have you squashed your commits?

**Q**: What version of *Node.js* you've used in the project? (`e.g. Node v10.11.0`)
**A**:v11.6.0

**Q**: Mention the *Issue Number*! (`e.g. Fixed #8`)
**A**:#96

- *Give additional information regarding the PR below:*

------------


Patch:
diff --git a/README.md b/README.md
index 993108b..da28479 100644
--- a/README.md
+++ b/README.md
@@ -30,7 +30,7 @@ $ npm install mathball --save
 
 - **Range of Numbers** - [rangePrime][7] - [rangeFibonacci][8] - [rangePalindrome][6]
 
-- **Mathematical Utilities** - [Complex][49] - [frequency][40] - [gcd][10] - [getAllPerm][55] - [isEven][11] - [isOdd][12] - [lcm][13] - [length][14] - [max][15] - [min][16] - [modInv][39] - [nCr][48] - [pow][46] - [sort][17] - [sum][18] - [toDegrees][50] - [toRadians][51]
+- **Mathematical Utilities** - [Complex][49] - [frequency][40] - [gcd][10] - [getAllPerm][55] - [isEven][11] - [isOdd][12] - [lcm][13] - [length][14] - [max][15] - [min][16] - [modInv][39] - [nCr][48] - [pow][46] - [sort][17] - [sum][18] - [toDegrees][50] - [toRadians][51] - [matrixMultiply][58]
 
 - **Data Structures** - [priorityQueue][44] - [stack][53] - [graph][57]
 
@@ -126,3 +126,4 @@ The MIT License 2019 - [Priyabrata Biswas][20].
 [93]: https://github.com/pbiswas101/Mathball/blob/master/src/triangular/index.js
 [56]: https://github.com/pbiswas101/Mathball/blob/master/src/ugly/index.js
 [57]: https://github.com/pbiswas101/Mathball/blob/master/src/graph/index.js
+[58]: https://github.com/pbiswas101/Mathball/blob/master/src/matrixMultiply/index.js
\ No newline at end of file
diff --git a/src/index.js b/src/index.js
index e148d21..67034a7 100644
--- a/src/index.js
+++ b/src/index.js
@@ -37,6 +37,7 @@ const frequency = require('./frequency'),
 	max = require('./max'),
 	min = require('./min'),
 	modInv = require('./modInv'),
+	matrixMultiply = require('./matrixMultiply'),
 	pow = require('./pow'),
 	sort = require('./sort'),
 	sum = require('./sum'),
@@ -73,6 +74,7 @@ module.exports = {
 	length,
 	isLucky: lucky.isLucky,
 	magic: magic.magic,
+	matrixMultiply,
 	max,
 	min,
 	nCr,
@@ -92,7 +94,7 @@ module.exports = {
 	sort,
 	sum,
 	toDegrees: toDegrees.toDegrees,
-	toRadians: toString.toRadians,
+	toRadians: toRadians.toRadians,
 	modInv: modInv.modInv,
 	frequency,
 	isDeficient,
diff --git a/src/matrixMultiply/index.js b/src/matrixMultiply/index.js
new file mode 100644
index 0000000..bafaa2e
--- /dev/null
+++ b/src/matrixMultiply/index.js
@@ -0,0 +1,50 @@
+const validate = require('../validation/number-array');
+
+function dimValidation(arr){
+    const dimension = arr[0].length;
+    for(let i = 1; i< arr.length; i++){
+
+        if(arr[i].length !== dimension){
+            return false;
+        }
+
+        else{
+            continue;
+        }
+    }
+    return true;
+}
+
+function multiply(arr1, arr2){
+
+    const noOfRows = arr1.length, noOfCols = arr2[0].length;
+    let temp = [], answer = [],sum = 0, i, j, k;
+
+    for(i = 0; i< noOfRows ;i++){
+        for(j = 0; j< noOfCols; j++){
+            for(k = 0; k< arr2.length; k++){
+                sum += arr1[i][k] * arr2[k][j];
+            }
+            temp.push(sum);
+            sum = 0;
+        }
+        answer.push(temp);
+        temp = [];
+    }
+
+    return answer;
+}
+
+module.exports = (arr1, arr2) => {
+
+    validate([].concat(...arr1), 'multiply');
+    validate([].concat(...arr2), 'multiply');
+    
+    if(!dimValidation(arr1) || !dimValidation(arr2) || (arr1[0].length !== arr2.length)){
+        return "Matrices are not compatible for multiplication";
+    }
+
+    else{
+        return multiply(arr1, arr2);
+    }
+};
\ No newline at end of file


Test patch:
diff --git a/test/matrixMultiply-spec.js b/test/matrixMultiply-spec.js
new file mode 100644
index 0000000..db89196
--- /dev/null
+++ b/test/matrixMultiply-spec.js
@@ -0,0 +1,25 @@
+const assert = require('assert'),
+    matrixMult = require('../src/matrixMultiply');
+
+describe('[Function: matrixMultiply]', () => {
+
+    it('should be a function', () => {
+        assert.strictEqual(typeof matrixMult, "function");
+    });
+
+    it('should return an error when matrices of wrong dimensions are entered', () => {
+        assert.strictEqual(matrixMult([[1,2,3],[1,2]], [[3],[1,2]]), "Matrices are not compatible for multiplication");
+    });
+
+    it('should return an error when a string is inserted instead of a number entered', () => {
+        assert.throws(() => matrixMult([[1,2,'a'],[1,2,4]]), TypeError);
+    });
+
+    it('should return an error when a string is passed', () => {
+        assert.throws(() => matrixMult("abs"), TypeError);
+    });
+
+    it('should return [ [ 114, 160, 60, 27 ],[ 74, 97, 73, 14 ], [ 119, 157, 112, 23 ] ] when [[12,7,3],[4 ,5,6],[7 ,8,9]] and [[5,8,1,2], [6,7,3,0], [4,5,9,1]]  are passed', () => {
+        assert.deepEqual(matrixMult([[12,7,3],[4 ,5,6],[7 ,8,9]],[[5,8,1,2], [6,7,3,0], [4,5,9,1]] ), [ [ 114, 160, 60, 27 ],[ 74, 97, 73, 14 ],  [ 119, 157, 112, 23 ] ]);
+    });
+});
\ No newline at end of file