├── .babelrc ├── .editorconfig ├── .gitignore ├── .npmignore ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── LICENSE ├── PULL_REQUEST_TEMPLATE.md ├── README.md ├── docs ├── Absolute │ └── index.md ├── Addition │ └── index.md ├── Average │ └── index.md ├── Binomial-Coeff │ └── index.md ├── Complex-Number │ └── index.md ├── Convert │ └── index.md ├── Count │ └── index.md ├── Divide │ └── index.md ├── Euler's Totient │ └── index.md ├── Find-nth-Number │ └── index.md ├── GCD │ └── index.md ├── Graph │ └── index.md ├── LCM │ └── index.md ├── Length │ └── index.md ├── Matrix │ └── index.md ├── Maximum │ └── index.md ├── Median │ └── index.md ├── Minimum │ └── index.md ├── Modular Inverse │ └── index.md ├── Modular-Exponent │ └── index.md ├── Multiply │ └── index.md ├── Number-Checker │ └── index.md ├── Performance │ └── index.md ├── Permute │ └── index.md ├── Pipe │ └── index.md ├── Priority-Queue │ └── index.md ├── Range-of-Number │ └── index.md ├── Set Bits │ └── index.md ├── Sort │ └── index.md ├── Stack │ └── index.md ├── Subtract │ └── index.md ├── Sum │ └── index.md └── _logo_ │ ├── mathball-banner.png │ └── mathball-logo.png ├── package-lock.json ├── package.json ├── src ├── Matrix │ └── index.js ├── absolute │ └── index.js ├── add │ └── index.js ├── armstrong │ └── index.js ├── automorphic │ └── index.js ├── average │ └── index.js ├── bell │ └── index.js ├── binary-search-tree │ └── index.js ├── binomial-coeff │ └── index.js ├── carmichael │ └── index.js ├── catalan │ └── index.js ├── check │ └── index.js ├── complex │ └── index.js ├── convert │ └── index.js ├── count │ └── index.js ├── deficient │ └── index.js ├── divide │ └── index.js ├── even │ └── index.js ├── factorial │ └── index.js ├── fibbinary │ └── index.js ├── fibonacci │ └── index.js ├── find │ └── index.js ├── frequency │ └── index.js ├── frugal │ └── index.js ├── gcd │ └── index.js ├── graph │ └── index.js ├── harshad │ └── index.js ├── hexagonal │ └── index.js ├── hoax │ └── index.js ├── index.js ├── inverse │ └── index.js ├── kaprekar │ └── index.js ├── lcm │ └── index.js ├── length │ └── index.js ├── lucky │ └── index.js ├── magic │ └── index.js ├── matrixChain │ └── index.js ├── matrixExponentiation │ └── index.js ├── max │ └── index.js ├── median │ └── index.js ├── min │ └── index.js ├── multiply │ └── index.js ├── neon │ └── index.js ├── odd │ └── index.js ├── padovan │ └── index.js ├── palindrome │ └── index.js ├── perfect │ └── index.js ├── performance │ └── index.js ├── permute │ └── index.js ├── pipe │ └── index.js ├── popcount │ └── index.js ├── pow │ └── index.js ├── prime-factor │ └── index.js ├── prime │ └── index.js ├── priority-queue │ └── index.js ├── range │ └── index.js ├── smart │ └── index.js ├── smith │ └── index.js ├── sort │ └── index.js ├── stack │ └── index.js ├── stormer │ └── index.js ├── subtract │ └── index.js ├── sum │ └── index.js ├── totient │ └── index.js ├── triangular │ └── index.js ├── ugly │ └── index.js ├── union │ └── index.js └── validation │ ├── argument-length.js │ ├── count.js │ ├── frequency.js │ ├── integer-array.js │ ├── integer-matrix.js │ ├── integer-object.js │ ├── integer.js │ ├── non-negative-integer.js │ ├── number-array.js │ ├── number.js │ ├── pipe.js │ ├── positive-integer.js │ └── string.js └── test ├── Matrix-spec.js ├── absolute-spec.js ├── add-spec.js ├── armstrong-spec.js ├── automorphic-spec.js ├── average-spec.js ├── bell-spec.js ├── binarysearchtree-spec.js ├── binomial-coeff-spec.js ├── carmichael-spec.js ├── catalan-spec.js ├── check-spec.js ├── complex-spec.js ├── convert-spec.js ├── count-spec.js ├── deficient-spec.js ├── divide-spec.js ├── even-spec.js ├── factorial-spec.js ├── fibbinary-spec.js ├── fibonacci-spec.js ├── find-spec.js ├── frequency-spec.js ├── frugal-spec.js ├── gcd-spec.js ├── graph-spec.js ├── harshad-spec.js ├── hexagonal-spec.js ├── hoax-spec.js ├── index-spec.js ├── inverse-spec.js ├── kaprekar-spec.js ├── lcm-spec.js ├── length-spec.js ├── lucky-spec.js ├── magic-spec.js ├── matrixChain-spec.js ├── matrixExponentiation-spec.js ├── max-spec.js ├── median-spec.js ├── min-spec.js ├── multiply-spec.js ├── neon-spec.js ├── odd-spec.js ├── padovan-spec.js ├── palindrome-spec.js ├── perfect-spec.js ├── performance-spec.js ├── permute-spec.js ├── pipe-spec.js ├── popcount-spec.js ├── pow-spec.js ├── prime-factor-spec.js ├── prime-spec.js ├── priority-queue-spec.js ├── range-spec.js ├── smart-spec.js ├── smith-spec.js ├── sort-spec.js ├── stack-spec.js ├── stormer-spec.js ├── subtract-spec.js ├── sum-spec.js ├── totient-spec.js ├── triangular-spec.js ├── ugly-spec.js └── union-spec.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["env"] 3 | } 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = tab 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage/ 19 | *.lcov 20 | 21 | # nyc test coverage 22 | .nyc_output 23 | 24 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 25 | .grunt 26 | 27 | # Bower dependency directory (https://bower.io/) 28 | bower_components 29 | 30 | # node-waf configuration 31 | .lock-wscript 32 | 33 | # Compiled binary addons (https://nodejs.org/api/addons.html) 34 | build/Release 35 | 36 | # Dependency directories 37 | node_modules/ 38 | jspm_packages/ 39 | 40 | # TypeScript v1 declaration files 41 | typings/ 42 | 43 | # Optional npm cache directory 44 | .npm 45 | 46 | # Optional eslint cache 47 | .eslintcache 48 | 49 | # Optional REPL history 50 | .node_repl_history 51 | 52 | # Output of 'npm pack' 53 | *.tgz 54 | 55 | # Yarn Integrity file 56 | .yarn-integrity 57 | 58 | # dotenv environment variables file 59 | .env 60 | 61 | # next.js build output 62 | .next 63 | 64 | # build directory 65 | lib/ 66 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Coverage directory used by tools like istanbul 2 | coverage/ 3 | *.lcov 4 | 5 | # nyc test coverage 6 | .nyc_output/ 7 | 8 | # media files 9 | docs/ 10 | 11 | # Dependency directories 12 | node_modules/ 13 | 14 | # source directory 15 | src/ 16 | 17 | # test directory 18 | test/ 19 | 20 | # Babel presets 21 | .babelrc 22 | 23 | # Editor configuration 24 | .editorconfig 25 | 26 | # Travis CI configuration 27 | .travis.yml 28 | 29 | # Theme configuration 30 | _config.yml 31 | 32 | # markdown files 33 | CONTRIBUTING.md 34 | ISSUE_TEMPLATE.md 35 | PULL_REQUEST_TEMPLATE.md 36 | CODE_OF_CONDUCT.md 37 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | 5 | script: 6 | - npm run test 7 | - npm run report-coverage 8 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at pbiswas101b@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Do the checklist before filing the issue: 2 | 3 | - [ ] Do you have Node.js and all the relevant dev-dependencies installed? 4 | - [ ] Is this a **bug fix**? 5 | - [ ] Is this an **enhancement**? 6 | - [ ] Is this a **feature request**? 7 | 8 | ###### 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 along with screenshots. 9 | 10 | - *Give your answer below:* 11 | 12 | ------------ 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Priyabrata Biswas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Do the checklist before submitting the PR: 2 | 3 | - [ ] Have you read the guidelines mentioned in **CONTRIBUTING.md**? 4 | - [ ] Have you squashed your commits? 5 | 6 | **Q**: What version of *Node.js* you've used in the project? (`e.g. Node v10.11.0`) 7 | **A**: 8 | 9 | **Q**: Mention the *Issue Number*! (`e.g. Fixed #8`) 10 | **A**: 11 | 12 | - *Give additional information regarding the PR below:* 13 | 14 | ------------ 15 | -------------------------------------------------------------------------------- /docs/Absolute/index.md: -------------------------------------------------------------------------------- 1 | # Abs 2 | The `M.abs()` function returns the absolute value of a Number, Array, String, or Complex Object. 3 | 4 | ### Syntax 5 | > M.abs(value) 6 | 7 | ### Arguments 8 | > Numbers
9 | > Strings
10 | > Arrays \ \
11 | > Objects \ 12 | 13 | ### Return 14 | > Number
15 | > String (in case of string inputs)
16 | 17 | ### Examples 18 | - Valid: 19 | ```js 20 | M.abs(-14); // 14 21 | M.abs(-12.24); // 12.24 22 | M.abs('-20'); // '20' 23 | M.abs([12, 9]]); // 0 24 | M.abs([[3, 2], [5, 4]]); // 2 25 | 26 | //const a = new M.Complex(7, 3); 27 | //const b = new M.Complex(4, 6); 28 | 29 | M.abs(a); // 7.62 30 | ``` 31 | - Invalid: 32 | ```js 33 | /* 34 | * TypeError: Invalid argument received: 35 | */ 36 | M.abs(true, false); 37 | 38 | /* 39 | * TypeError: Invalid argument received: 40 | */ 41 | M.abs(); 42 | M.abs(1, 3); 43 | M.abs('4', '3'); 44 | M.abs([5, 3, 4], [1, 2, 3]); 45 | M.abs([[5]], [[4]]); 46 | M.abs(a, b); 47 | /* 48 | * TypeError: Invalid argument received: 49 | * 'function' only accepts parameters of similar length! 50 | */ 51 | 52 | /*Arrays*/ 53 | M.abs([1,2,3,4,5], [1,2,3,4]); 54 | M.abs([[1]], [[1,2,3]]); 55 | 56 | ``` 57 | 58 | ### Info: 59 | 60 | Implemented using JavaScript's built-in `reduce`, `forEach`, `toString`, `filter`, `map` function. 61 | 62 | -------------------------------------------------------------------------------- /docs/Addition/index.md: -------------------------------------------------------------------------------- 1 | # Add 2 | The `M.add()` function returns the sum of two or more Numbers, Arrays, Strings, or Complex Objects. 3 | 4 | ### Syntax 5 | > M.add(value1, value2, ...) 6 | 7 | ### Arguments 8 | > Numbers
9 | > Strings
10 | > Arrays \ \
11 | > Objects \ 12 | 13 | ### Return 14 | > Number (in case of number inputs)
15 | > String (in case of string inputs)
16 | > Array (in case of array inputs)
17 | > Object (in case of object inputs) 18 | 19 | ### Examples 20 | - Valid: 21 | ```js 22 | M.add(1, 2, 3, 4, 5, 6); // 21 23 | M.add(1.2, 2.3, 3.4, 4.5); // 11.4 24 | M.add('1', '2, '3', '4'); // '10' 25 | M.add([2, 4], [2, 4]); // [4, 8] 26 | M.add([[1, 2, 3]], [[1, 2, 3]]); // [[2, 4, 6]] 27 | ``` 28 | - Invalid: 29 | ```js 30 | /* 31 | * TypeError: Invalid argument received: 32 | */ 33 | M.add(true, false); 34 | 35 | /* 36 | * TypeError: Invalid argument received: 37 | * 'add()' only accepts 2 or more parameters! 38 | */ 39 | M.add(); 40 | M.add(1); 41 | M.add(1.3); 42 | M.add('4'); 43 | M.add([5, 3, 4]); 44 | M.add([[5]]); 45 | 46 | /* 47 | * TypeError: Invalid argument received: 48 | * 'function' only accepts parameters of similar types! 49 | */ 50 | 51 | /*Numbers*/ 52 | M.add(5, 2, '1', 7); 53 | M.add(1.2, 1.4, [1,2,3], 5.6); 54 | 55 | /*Strings*/ 56 | M.add('1', '2', '3', 4, 5); 57 | 58 | /*Arrays*/ 59 | M.add([1,2,3,4], ['1','2','3','4']); 60 | M.add([[1, 2]], [[3, 4]], [['4', '5']]); 61 | 62 | /*Complex Objects*/ 63 | 64 | //const a = new M.Complex(4,3); 65 | //const b = new M.Complex(5,4); 66 | M.add(a, b, {p: 30, q: 40}); 67 | 68 | /* 69 | * TypeError: Invalid argument received: 70 | * 'function' only accepts parameters of similar length! 71 | */ 72 | 73 | /*Arrays*/ 74 | M.add([1,2,3,4,5], [1,2,3,4]); 75 | M.add([[1]], [[1,2,3]], [[8]]); 76 | 77 | ``` 78 | 79 | ### Info: 80 | Implemented using JavaScript's built-in `reduce`, `forEach`, 81 | `toString`, `filter`, `map` function. 82 | 83 | -------------------------------------------------------------------------------- /docs/Average/index.md: -------------------------------------------------------------------------------- 1 | # Average 2 | The `M.avg()` function returns the average of the numbers provided. 3 | 4 | ### Syntax 5 | > M.avg([value1, value2, ...]) 6 | 7 | ### Arguments 8 | > Array \ 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.avg([1, 0, 5, 6]); // 3 17 | M.avg([8, 7]); // 7.5 18 | ``` 19 | - Invalid: 20 | ```js 21 | /* 22 | * TypeError: Invalid argument received: 23 | * 'avg()' only accept an array of real numbers! 24 | */ 25 | M.avg(1, 2); 26 | M.avg([NaN, 5, Infinity]); 27 | M.avg(['foo', 'bar']); 28 | ``` 29 | 30 | ### Info: 31 | Implemented using JavaScript's built-in `reduce` function. 32 | 33 | ------ 34 | -------------------------------------------------------------------------------- /docs/Binomial-Coeff/index.md: -------------------------------------------------------------------------------- 1 | # Binomial Coefficient 2 | The ``` M.nCr(n,r) ``` function return the coefficiant of xr in the expansion of (1+x)n . 3 | 4 | ### Syntax 5 | > M.nCr(value of n,value of r) 6 | 7 | ### Arguments 8 | > Number 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.nCr(4,2); // 6 17 | M.nCr(5,2); // 10 18 | M.nCr(3,1); // 3 19 | ``` 20 | - Invalid: 21 | ```js 22 | /* 23 | * TypeError: Invalid argument received: 24 | * 'nCr(n,k)' only accept positive real numbers where n >= r must hold true. 25 | */ 26 | M.nCr(3,5); 27 | M.nCr(-2,5); 28 | M.nCr(2.02,3); 29 | ``` 30 | 31 | ### Info: 32 | Implemented using JavaScript's `if()` condition and `for()` loop. 33 | 34 | ------ 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /docs/Convert/index.md: -------------------------------------------------------------------------------- 1 | # Average 2 | The `M.convert()` function returns a number which has been converted from degree to radian or vice versa. 3 | 4 | ### Syntax 5 | > M.permute(argument1, argument2, argument3) 6 | 7 | ### Argument1 8 | > Number 9 | 10 | ### Argument2 11 | > Object 12 | 13 | ### Argument3 14 | > Number 15 | 16 | ### Return 17 | > Number 18 | 19 | ### Examples 20 | - Valid: 21 | ```js 22 | M.convert(60, {from: 'degree', to: 'radian'}) // 1.0471975511965976 23 | M.convert(1.0471975511965976, {from: 'radian', to: 'degree'}) // 59.99999999999999 24 | M.convert(180, {from: 'degree', to: 'radian'}, 0) // 3.141592653589793 25 | M.convert(180, {from: 'degree', to: 'radian'}, 8) // 3.14159265 26 | M.convert(1.309, {from: 'radian', to: 'degree'}) // 75.00017538262475 27 | M.convert(1.309, {from: 'radian', to: 'degree'}, 0) // 75.00017538262475 28 | M.convert(1.309, {from: 'radian', to: 'degree'}, 6) // 75.000175 29 | ``` 30 | - Invalid: 31 | ```js 32 | /* 33 | * TypeError: Invalid argument received: 34 | * 'convert()' only accept a number! 35 | */ 36 | M.convert("31", {from: 'degree', to: 'radian'}); 37 | M.convert(true, {from: 'degree', to: 'radian'}); 38 | 39 | /* 40 | * TypeError: Invalid argument received: 41 | * in convert() must be an object! 42 | */ 43 | M.convert(60, true, 6); 44 | 45 | /* 46 | * ReferenceError: Invalid argument received: 47 | * in convert() must contain to and from fields! 48 | */ 49 | M.convert(180, {from: 'radian', an: 'degree'}); 50 | M.convert(180, {a: 'radian', an: 'degree'}, 6); 51 | 52 | /* 53 | * TypeError: Invalid argument received: 54 | * 'convert()' only accept a non-negative integer! 55 | */ 56 | M.convert(31, {from: 'degree', to: 'radian'}, true); 57 | M.convert(60, {from: 'degree', to: 'radian'}, 'a'); 58 | 59 | ``` 60 | 61 | ### Info: 62 | Implemented using JavaScript's built-in `parseFloat`,`toFixed`,`hasOwnProperty` functions. 63 | 64 | ------ 65 | -------------------------------------------------------------------------------- /docs/Count/index.md: -------------------------------------------------------------------------------- 1 | # Count 2 | The `M.count()` function returns the number of occurences of the second argument in the first argument. 3 | 4 | ### Syntax 5 | > M.count (argument1, argument2) 6 | 7 | ### Argument1 8 | > Array 9 | > String 10 | > Object 11 | 12 | ### Argument2 13 | > Any datatype 14 | 15 | ### Return 16 | > Number 17 | 18 | ### Examples 19 | - Valid: 20 | ```js 21 | /*Arrays*/ 22 | M.count([1, 2, 3, 1, 2, 4, 5, 1], 1) // 3 23 | M.count(['a', 'y', 'a', 'b', 'c', 'z', 'i'], 'a') // 2 24 | M.count(['hello', 'world', 'lorem', 'ipsum', 'hello'], 'world'); // 1 25 | M.count(['hello', 5, 'lorem', 7, 'ipsum', 'hello'], 'world'); // 0 26 | M.count([true, 5, 'lorem', 7, 'ipsum', 'hello'], true); // 1 27 | 28 | /*Strings*/ 29 | M.count('banana', 'ana'); // 2 30 | M.count('banana', 'an'); // 2 31 | M.count('banana', 'a'); // 3 32 | 33 | /*Objects*/ 34 | M.count({'lorem': 'ipsum', 'hello': 'world'}, 'world'); // 1 35 | M.count({'foo': 'bar', 'spam': 'egg', 'lorem': 'bar'}, 'bar'); // 2 36 | M.count({'ipsum': [true, 1]}, [true, 1]); // 1 37 | ``` 38 | - Invalid: 39 | ```js 40 | /* 41 | * TypeError: Invalid argument received: 42 | * 'count()' only accept an Array, String or an Object! 43 | */ 44 | M.count(); 45 | M.count(true); 46 | M.count(23); 47 | 48 | /* 49 | * TypeError: Invalid argument received: 50 | * 'count()' doesnot accept more than 2 parameters! 51 | */ 52 | 53 | ``` 54 | 55 | ### Info: 56 | Implemented using JavaScript's built-in `filter`, `hasOwnProperty`, `indexOf`, `keys` functions. 57 | 58 | ------ 59 | 60 | -------------------------------------------------------------------------------- /docs/Divide/index.md: -------------------------------------------------------------------------------- 1 | # Div 2 | The `M.div()` function returns the division of two Numbers, Arrays, Strings, or Complex Objects. 3 | 4 | ### Syntax 5 | > M.div(value1, value2) 6 | 7 | ### Arguments 8 | > Numbers
9 | > Strings
10 | > Arrays \ \
11 | > Objects \ 12 | 13 | ### Return 14 | > Number (in case of number inputs)
15 | > String (in case of string inputs)
16 | > Array (in case of array inputs)
17 | > Object (in case of object inputs) 18 | 19 | ### Examples 20 | - Valid: 21 | ```js 22 | M.div(4, 2); // 2 23 | M.div(8.4, 2.0); // 4.2 24 | M.div('8.4', '2.0'); // '4.2' 25 | M.div([12, 9], [6, 3]); // [2, 3] 26 | M.div([[4, 3],[3, 2]], [[4, 3], [3,2]]); // [[1, 0], [0, 1]] 27 | 28 | //const a = new M.Complex(7, 3); 29 | //const b = new M.Complex(4, 6); 30 | 31 | M.div(a,b); // {re: 0.77, im: -0.58} 32 | ``` 33 | - Invalid: 34 | ```js 35 | /* 36 | * TypeError: Invalid argument received: 37 | */ 38 | M.div(true, false); 39 | 40 | /* 41 | * TypeError: Invalid argument received: 42 | * 'div()' only accepts only 2 parameters! 43 | */ 44 | M.div(); 45 | M.div(1); 46 | M.div(1.3); 47 | M.div('4'); 48 | M.div([5, 3, 4]); 49 | M.div([[5]]); 50 | 51 | /* 52 | * TypeError: Invalid argument received: 53 | * 'function' only accepts parameters of similar types! 54 | */ 55 | 56 | /*Numbers*/ 57 | M.div(5, '1'); 58 | M.div(1.2, '2'); 59 | 60 | /*Strings*/ 61 | M.div('6', 1); 62 | 63 | /*Arrays*/ 64 | M.div([1,2,3,4], ['1','2','3','4']); 65 | M.div([[1, 2]], [['4', '5']]); 66 | 67 | /*Complex Objects*/ 68 | 69 | M.div(a, b, {p: 30, q: 40}); 70 | M.div(a, b, {p: 30, q: 40}); 71 | 72 | /* 73 | * TypeError: Invalid argument received: 74 | * 'function' only accepts parameters of similar length! 75 | */ 76 | 77 | /*Arrays*/ 78 | M.div([1,2,3,4,5], [1,2,3,4]); 79 | M.div([[1]], [[1,2,3]]); 80 | 81 | ``` 82 | 83 | ### Info: 84 | Implemented using JavaScript's built-in `reduce`, `forEach`, `toString`, `filter`, `map` function. 85 | 86 | -------------------------------------------------------------------------------- /docs/Euler's Totient/index.md: -------------------------------------------------------------------------------- 1 | # Euler's Totient
2 | The ``` M.phi() ``` function for an input n returns count of numbers in {1, 2, 3, …, n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1 3 | 4 | ### Syntax 5 | > M.phi(num) 6 | 7 | ### Arguments 8 | > Number 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.phi(9); // 6 17 | M.phi(0); // 0 18 | M.phi(10) // 4 19 | ``` 20 | - Invalid: 21 | ```js 22 | /* 23 | * TypeError: Invalid argument received: 24 | * 'phi()' only accept a non-negative-integer! 25 | */ 26 | M.phi("31") 27 | M.phi(true) 28 | M.phi(31.101996) 29 | M.phi(-20) 30 | M.phi(0) 31 | ``` 32 | 33 | ### Info: 34 | Implemented using Euler’s product formula. 35 | 36 | ------ 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /docs/GCD/index.md: -------------------------------------------------------------------------------- 1 | # GCD 2 | The `M.gcd()` function returns the gcd of two or more numbers. 3 | 4 | ### Syntax 5 | > M.gcd([value1, value2, ...]) 6 | 7 | ### Arguments 8 | > Array \ 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.gcd([12, 18, 24]); // 6 17 | M.gcd([8, 7]); // 1 18 | ``` 19 | - Invalid: 20 | ```js 21 | /* 22 | * TypeError: Invalid argument received: 23 | * 'gcd()' only accept an array of positive real numbers! 24 | */ 25 | M.gcd(-20, 24); 26 | M.gcd([NaN, 5, Infinity]); 27 | M.gcd(['foo', 'bar']); 28 | ``` 29 | 30 | ### Info: 31 | Implemented using JavaScript's built-in `reduce` function. 32 | 33 | ------ 34 | -------------------------------------------------------------------------------- /docs/LCM/index.md: -------------------------------------------------------------------------------- 1 | # Sum 2 | The `M.lcm()` function returns the lcm of two or more numbers. 3 | 4 | ### Syntax 5 | > M.lcm([value1, value2, ...]) 6 | 7 | ### Arguments 8 | > Array \ 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.lcm([2, 3, 5, 15]); // 30 17 | M.lcm([8, 7]); // 56 18 | ``` 19 | - Invalid: 20 | ```js 21 | /* 22 | * TypeError: Invalid argument received: 23 | * 'lcm()' only accept an array of positive real numbers! 24 | */ 25 | M.lcm([-40,8]); 26 | M.lcm([NaN, 5, Infinity]); 27 | M.lcm(['foo', 'bar']); 28 | ``` 29 | 30 | ### Info: 31 | Implemented using JavaScript's built-in `reduce` function. 32 | 33 | ------ 34 | -------------------------------------------------------------------------------- /docs/Length/index.md: -------------------------------------------------------------------------------- 1 | # Count 2 | The `M.length()` function returns the length of argument provided. 3 | 4 | ### Syntax 5 | > M.length (value) 6 | 7 | ### Arguments 8 | > Number 9 | > Array 10 | > String 11 | > Object 12 | 13 | ### Return 14 | > Number 15 | 16 | ### Examples 17 | - Valid: 18 | ```js 19 | M.length(2345); // 4 20 | M.length(123.456); // 6 21 | M.length([1, 2, 3, 1, 2, 4, 5, 1]); // 8 22 | M.length('helloworld'); // 10 23 | M.length({'lorem': 'ipsum', 'hello': 'world'}); // 2 24 | ``` 25 | - Invalid: 26 | ```js 27 | /* 28 | * TypeError: Invalid argument received: 29 | * 'length()' only accept either a real number, 30 | * string, object or array! 31 | */ 32 | M.length(); 33 | M.length(true); 34 | ``` 35 | 36 | ### Info: 37 | Implemented using JavaScript's built-in `split`, `toString`, `splice`, 38 | `indexOf`, `keys` functions 39 | 40 | ------ 41 | 42 | -------------------------------------------------------------------------------- /docs/Maximum/index.md: -------------------------------------------------------------------------------- 1 | # Maximum 2 | The `M.max()` function returns the maximum of two or more numbers. 3 | 4 | ### Syntax 5 | > M.max([value1, value2, ...]) 6 | 7 | ### Arguments 8 | > Array \ 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.max([1, 0, -5, 6]); // 6 17 | M.max([8, 7]); // 8 18 | ``` 19 | - Invalid: 20 | ```js 21 | /* 22 | * TypeError: Invalid argument received: 23 | * 'max()' only accept an array of real numbers! 24 | */ 25 | M.max(1, 2); 26 | M.max([NaN, 5, Infinity]); 27 | M.max(['foo', 'bar']) 28 | ``` 29 | 30 | ### Info: 31 | Implemented using JavaScript's built-in `Math.max()` function. 32 | 33 | ------ 34 | -------------------------------------------------------------------------------- /docs/Median/index.md: -------------------------------------------------------------------------------- 1 | # Median 2 | The `M.median()` function returns the median of the numbers provided. 3 | 4 | ### Syntax 5 | > M.median([value1, value2, ...]) 6 | 7 | ### Arguments 8 | > Array \ 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.median([1, 3, 4, 2, 6, 5, 8, 7]); // 4.5 17 | M.median([4, 4, 4, 4]); // 4 18 | ``` 19 | - Invalid: 20 | ```js 21 | /* 22 | * TypeError: Invalid argument received: 23 | * 'median()' only accept an array of real numbers! 24 | */ 25 | M.median(1, 2); 26 | M.median([NaN, 5, Infinity]); 27 | M.median(['foo', 'bar']); 28 | ``` 29 | 30 | ### Info: 31 | Implemented using JavaScript's built-in `Math.sort()` 32 | `Math.floor()` function. 33 | 34 | ------ 35 | -------------------------------------------------------------------------------- /docs/Minimum/index.md: -------------------------------------------------------------------------------- 1 | # Minimum 2 | The `M.min()` function returns the minimum of two or more numbers. 3 | 4 | ### Syntax 5 | > M.min([value1, value2, ...]) 6 | 7 | ### Arguments 8 | > Array \ 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.min([1, 0, 5, 6]); // 0 17 | M.min([8, 7]); // 7 18 | ``` 19 | - Invalid: 20 | ```js 21 | /* 22 | * TypeError: Invalid argument received: 23 | * 'min()' only accept an array of real numbers! 24 | */ 25 | M.min(1, 2); 26 | M.min([NaN, 5, Infinity]); 27 | M.min(['foo', 'bar']) 28 | ``` 29 | 30 | ### Info: 31 | Implemented using JavaScript's built-in `Math.min()` function. 32 | 33 | ------ 34 | -------------------------------------------------------------------------------- /docs/Modular Inverse/index.md: -------------------------------------------------------------------------------- 1 | # Modular Inverse 2 | The `M.inverse()` function returns the modular inverse of the arguments. 3 | 4 | ### Syntax 5 | > M.inverse (value1, value2) 6 | 7 | ### Arguments 8 | > Number 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.inverse(3, 11); // 4 17 | M.inverse(6, 9); // 0 18 | M.inverse(10, 17); // 12 19 | ``` 20 | - Invalid: 21 | ```js 22 | /* 23 | * TypeError: Invalid argument received: 24 | * 'inverse()' only accept positive integers! 25 | */ 26 | M.inverse(); 27 | M.inverse(-20, 10); 28 | M.inverse(2.4, 3.5); 29 | M.inverse(true, false); 30 | M.inverse('helloworld'); 31 | M.inverse({'lorem': 'ipsum', 'hello': 'world'}); 32 | ``` 33 | 34 | ### Info 35 | Implemented using Mathball's built-in 'gcd' function. 36 | 37 | ------ 38 | -------------------------------------------------------------------------------- /docs/Modular-Exponent/index.md: -------------------------------------------------------------------------------- 1 | # Modular Exponentiation 2 | The `M.pow()` function returns the modular exponent of the arguments. 3 | 4 | ### Syntax 5 | > M.pow (base, expo, modulo) 6 | 7 | ### Arguments 8 | > Number 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.pow (6, 3, 5); // 1 17 | M.pow (8, 4, 5); // 1 18 | M.pow (5, 3); // 125 19 | ``` 20 | - Invalid: 21 | ```js 22 | /* 23 | * TypeError: Invalid argument received: 24 | * 'pow ()' only accept a non-negative integer! 25 | */ 26 | M.pow (); 27 | M.pow(-10); 28 | M.pow (20, 5, -9); 29 | M.pow (2.4, 3.5); 30 | M.pow (true, false); 31 | M.pow ('31', '45', '2'); 32 | ``` 33 | 34 | ### Info 35 | Implemented using JavaScript's basic arithmetic operations. 36 | 37 | ------ 38 | -------------------------------------------------------------------------------- /docs/Multiply/index.md: -------------------------------------------------------------------------------- 1 | # Mul 2 | The `M.mul()` function returns the product of two Numbers, Arrays, Strings, or Complex Objects. 3 | 4 | ### Syntax 5 | > M.mul(value1, value2) 6 | 7 | ### Arguments 8 | > Numbers
9 | > Strings
10 | > Arrays \ \
11 | > Objects \ 12 | 13 | ### Return 14 | > Number (in case of number inputs)
15 | > String (in case of string inputs)
16 | > Array (in case of array inputs)
17 | > Object (in case of object inputs) 18 | 19 | ### Examples 20 | - Valid: 21 | ```js 22 | M.mul(2, 3); // 6 23 | M.mul(4.5, 2.3); // 10.35 24 | M.mul('20', '10'); // '200' 25 | M.mul([12, 9], [6, 6]); // [72, 54] 26 | M.mul([[1,2], [3,4]], [[2, 0], [1, 2]]); // [[4, 4], [10, 8]] 27 | 28 | //const a = new M.Complex(7, 3); 29 | //const b = new M.Complex(4, 6); 30 | 31 | M.mul(a,b); // {re: 28, im: 18} 32 | ``` 33 | - Invalid: 34 | ```js 35 | /* 36 | * TypeError: Invalid argument received: 37 | */ 38 | M.mul(true, false); 39 | 40 | /* 41 | * TypeError: Invalid argument received: 42 | * 'mul()' only accepts only 2 parameters! 43 | */ 44 | M.mul(); 45 | M.mul(1); 46 | M.mul(1.3); 47 | M.mul('4'); 48 | M.mul([5, 3, 4]); 49 | M.mul([[5]]); 50 | /* 51 | * TypeError: Invalid argument received: 52 | * 'function' only accepts parameters of similar types! 53 | */ 54 | 55 | /*Numbers*/ 56 | M.mul(5, '1'); 57 | M.mul(1.2, '2'); 58 | 59 | /*Strings*/ 60 | M.mul('6', 1); 61 | 62 | /*Arrays*/ 63 | M.mul([1,2,3,4], ['1','2','3','4']); 64 | M.mul([[1, 2]], [['4', '5']]); 65 | 66 | /*Complex Objects*/ 67 | 68 | M.mul(a, b, {p: 30, q: 40}); 69 | 70 | /* 71 | * TypeError: Invalid argument received: 72 | * 'function' only accepts parameters of similar length! 73 | */ 74 | 75 | /*Arrays*/ 76 | M.mul([1,2,3,4,5], [1,2,3,4]); 77 | M.mul([[1]], [[1,2,3]]); 78 | 79 | ``` 80 | 81 | ### Info: 82 | Implemented using JavaScript's built-in `reduce`, `forEach`, `toString`, `filter`, `map` function. 83 | 84 | -------------------------------------------------------------------------------- /docs/Performance/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fave77/Mathball/f3cfa8abb7415ec5fee88031e5e8af78a938f586/docs/Performance/index.md -------------------------------------------------------------------------------- /docs/Permute/index.md: -------------------------------------------------------------------------------- 1 | # Average 2 | The `M.permute()` function returns all permutations of the given string. 3 | 4 | ### Syntax 5 | > M.permute("value") 6 | 7 | ### Arguments 8 | > String 9 | 10 | ### Return 11 | > Array 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.permute("abc") // "abc","acb","bac","bca","cab","cba" 17 | M.permute("123") // "123","132","213","231","312","321" 18 | M.permute("4") // "4" 19 | M.permute("a") // "a" 20 | M.permute("15") // "15","51" 21 | M.permute("ab") // "ab","ba" 22 | ``` 23 | - Invalid: 24 | ```js 25 | /* 26 | * TypeError: Invalid argument received: 27 | * 'permute()' only accept a string! 28 | */ 29 | M.permute(123); 30 | M.permute(-20); 31 | M.permute(0.254); 32 | M.permute(["foo", "bar"]); 33 | ``` 34 | 35 | ### Info: 36 | Implemented using JavaScript's built-in `split`,`shift`,`splice`,`push`,`map`,`filter`,`sort`,`join`,`indexOf` functions. 37 | 38 | ------ 39 | -------------------------------------------------------------------------------- /docs/Pipe/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fave77/Mathball/f3cfa8abb7415ec5fee88031e5e8af78a938f586/docs/Pipe/index.md -------------------------------------------------------------------------------- /docs/Range-of-Number/index.md: -------------------------------------------------------------------------------- 1 | # range 2 | The `M.range()` takes a type of number as argument such as prime-numbers & returns all those numbers in the range provided as arguments , like `M.range('prime')(1,5)` will return all the prime numbers between 1 & 5. 3 | 4 | ### Syntax 5 | > M.range('number-type') 6 | 7 | ### Arguments 8 | > 9 | 10 | ### Return 11 | > e.g. rangePrime 12 | rangePrime(m,n) will return all prime numbers between m & n. 13 | 14 | ### Numbers Supported 15 | - [Palindrome](https://en.wikipedia.org/wiki/Palindromic_number) 16 | - [Prime](https://en.wikipedia.org/wiki/Prime_number) 17 | 18 | 19 | ### Examples 20 | * Palindrome Numbers 21 | * Valid: 22 | ```js 23 | const rangePalindrome = M.range('palindrome'); 24 | rangePalindrome(1,5); // [1,2,3,4] 25 | rangePalindrome(10,20); // [11] 26 | rangePalindrome(0,18); // [0,1,2,3,4,5,6,7,8,9,11] 27 | ``` 28 | * Invalid 29 | ```js 30 | rangePalindrome([3, 4]); //Typerror 31 | rangePalindrome(true); //Typerror 32 | rangePalindrome(3.141718); //Typerror 33 | ``` 34 | 35 | * Prime Numbers 36 | * Valid: 37 | ```js 38 | const rangePrime = M.range('prime'); 39 | rangePrime(25); // [ 2, 3, 5, 7, 11, 13, 17, 19, 23 ] 40 | rangePrime(7); // [ 2, 3, 5, 7 ] 41 | rangePrime(14); // [ 2, 3, 5, 7, 11, 13] 42 | ``` 43 | * Invalid 44 | ```js 45 | rangePrime("31"); //Typerror 46 | rangePrime(true); //Typerror 47 | rangePrime(-20); //Typerror 48 | ``` 49 | 50 | ### Info: 51 | Implemented using JavaScript's ability to export functions. 52 | Similar methods can be rangeed here: 53 | [Find-nth-Number](https://github.com/pbiswas101/Mathball/tree/master/docs/Find-nth-Number) 54 | [Number-Checker](https://github.com/pbiswas101/Mathball/tree/master/docs/Number-Checker) 55 | 56 | ------ 57 | -------------------------------------------------------------------------------- /docs/Set Bits/index.md: -------------------------------------------------------------------------------- 1 | # Set Bits 2 | The `M.popcount()` function returns the count of the total number of 1s present in the binary representation of an integer. 3 | 4 | ### Syntax 5 | > M.popcount(value) 6 | 7 | ### Arguments 8 | > Number 9 | 10 | ### Return 11 | > Non-negative Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.popcount(13); // 3 17 | M.popcount(6); // 2 18 | ``` 19 | - Invalid: 20 | ```js 21 | /* 22 | * TypeError: Invalid argument received: 23 | * 'popcount()' only accept an integer value. 24 | */ 25 | M.popcount(1, 2); 26 | M.popcount('one'); 27 | ``` 28 | 29 | ### Info: 30 | Implemented using JavaScript's `while()` loop. 31 | 32 | ------ 33 | 34 | -------------------------------------------------------------------------------- /docs/Sort/index.md: -------------------------------------------------------------------------------- 1 | # Sort
2 | The ``` M.sort() ``` function returns a sorted array of integers in either ascending or descending order. 3 | 4 | ### Syntax 5 | > M.sort(value1,value2) 6 | 7 | ### Arguments 8 | > Array , Boolean 9 | 10 | ### Return 11 | > Array 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.sort([2, 30, 5, 15,],true); // [2,5,15,30] true=ascending order 17 | M.sort([8, -7,-5,40,9],false); // [40,9,8,-5,-7] false=descending order 18 | M.sort([8, -7,-5,40,9]); // [-7,-5,8,9,40] default=ascending order 19 | ``` 20 | - Invalid: 21 | ```js 22 | /* 23 | * TypeError: Invalid argument received: 24 | * 'lcm()' only accept an array of positive real numbers! 25 | */ 26 | M.sort([true,8],true); 27 | M.sort([NaN, 5, Infinity],true); 28 | M.sort(['foo', 'bar'],false); 29 | ``` 30 | 31 | ### Info: 32 | Implemented using JavaScript's built-in `sort` function. 33 | 34 | ------ 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /docs/Subtract/index.md: -------------------------------------------------------------------------------- 1 | # Sub 2 | The `M.sub()` function returns the difference of two Numbers, Arrays, Strings, or Complex Objects. 3 | 4 | ### Syntax 5 | > M.sub(value1, value2) 6 | 7 | ### Arguments 8 | > Numbers
9 | > Strings
10 | > Arrays \ \
11 | > Objects \ 12 | 13 | ### Return 14 | > Number (in case of number inputs)
15 | > String (in case of string inputs)
16 | > Array (in case of array inputs)
17 | > Object (in case of object inputs) 18 | 19 | ### Examples 20 | - Valid: 21 | ```js 22 | M.sub(50, 29); // 21 23 | M.sub(4.5, 2.3); // 2.2 24 | M.sub('20', '10'); // '10' 25 | M.sub([12, 4], [2, 3]); // [10, 1] 26 | M.sub([[5, 3]], [[4, 1]]); // [[1, 2]] 27 | 28 | //const a = new M.Complex(7, 3); 29 | //const b = new M.Complex(4, 6); 30 | 31 | M.sub(a,b); // {re: 3, im: -3} 32 | ``` 33 | - Invalid: 34 | ```js 35 | /* 36 | * TypeError: Invalid argument received: 37 | */ 38 | M.sub(true, false); 39 | 40 | /* 41 | * TypeError: Invalid argument received: 42 | * 'sub()' only accepts only 2 parameters! 43 | */ 44 | M.sub(); 45 | M.sub(1); 46 | M.sub(1.3); 47 | M.sub('4'); 48 | M.sub([5, 3, 4]); 49 | M.sub([[5]]); 50 | 51 | /* 52 | * TypeError: Invalid argument received: 53 | * 'function' only accepts parameters of similar types! 54 | */ 55 | 56 | /*Numbers*/ 57 | M.sub(5, '1'); 58 | M.sub(1.2, '2'); 59 | 60 | /*Strings*/ 61 | M.sub('6', 1); 62 | 63 | /*Arrays*/ 64 | M.sub([1,2,3,4], ['1','2','3','4']); 65 | M.sub([[1, 2]], [['4', '5']]); 66 | 67 | /*Complex Objects*/ 68 | 69 | M.sub(a, b, {p: 30, q: 40}); 70 | 71 | /* 72 | * TypeError: Invalid argument received: 73 | * 'function' only accepts parameters of similar length! 74 | */ 75 | 76 | /*Arrays*/ 77 | M.sub([1,2,3,4,5], [1,2,3,4]); 78 | M.sub([[1]], [[1,2,3]]); 79 | 80 | ``` 81 | 82 | ### Info: 83 | Implemented using JavaScript's built-in `reduce`, `forEach`, `toString`, `filter`, `map` function. 84 | 85 | -------------------------------------------------------------------------------- /docs/Sum/index.md: -------------------------------------------------------------------------------- 1 | # Sum 2 | The `M.sum()` function returns the sum of two or more numbers. 3 | 4 | ### Syntax 5 | > M.sum([value1, value2, ...]) 6 | 7 | ### Arguments 8 | > Array \ 9 | 10 | ### Return 11 | > Number 12 | 13 | ### Examples 14 | - Valid: 15 | ```js 16 | M.sum([1, 0, 5, 6]); // 12 17 | M.sum([8, 7]); // 15 18 | ``` 19 | - Invalid: 20 | ```js 21 | /* 22 | * TypeError: Invalid argument received: 23 | * 'sum()' only accept an array of real numbers! 24 | */ 25 | M.sum(1, 2); 26 | M.sum([NaN, 5, Infinity]); 27 | M.sum(['foo', 'bar']); 28 | ``` 29 | 30 | ### Info: 31 | Implemented using JavaScript's built-in `reduce` function. 32 | 33 | ------ 34 | -------------------------------------------------------------------------------- /docs/_logo_/mathball-banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fave77/Mathball/f3cfa8abb7415ec5fee88031e5e8af78a938f586/docs/_logo_/mathball-banner.png -------------------------------------------------------------------------------- /docs/_logo_/mathball-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fave77/Mathball/f3cfa8abb7415ec5fee88031e5e8af78a938f586/docs/_logo_/mathball-logo.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mathball", 3 | "version": "0.4.0", 4 | "description": "A JavaScript library for Competitive Programming", 5 | "keywords": [ 6 | "competitive-programming", 7 | "computer-science", 8 | "algorithms", 9 | "data-structures", 10 | "javascript", 11 | "mathematical-utilities" 12 | ], 13 | "main": "lib/index.js", 14 | "scripts": { 15 | "build": "babel ./src -d ./lib", 16 | "test": "nyc mocha --require babel-core/register", 17 | "report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", 18 | "prepublishOnly": "npm run build" 19 | }, 20 | "nyc": { 21 | "reporter": [ 22 | "lcov", 23 | "text" 24 | ] 25 | }, 26 | "devDependencies": { 27 | "babel-cli": "^6.26.0", 28 | "babel-preset-env": "^1.7.0", 29 | "codecov": "^3.1.0", 30 | "mocha": "^5.2.0", 31 | "nyc": "^13.3.0" 32 | }, 33 | "repository": { 34 | "type": "git", 35 | "url": "git+https://github.com/pbiswas101/Mathball.git" 36 | }, 37 | "bugs": { 38 | "url": "https://github.com/pbiswas101/Mathball/issues" 39 | }, 40 | "author": "Priyabrata Biswas", 41 | "license": "MIT" 42 | } 43 | -------------------------------------------------------------------------------- /src/absolute/index.js: -------------------------------------------------------------------------------- 1 | /*Absolute value across all data-types and data-structures 2 | *Function: M.abs(m,n) 3 | */ 4 | const Complex = require('../complex'); 5 | const validate = require('../validation/argument-length'); 6 | 7 | const abs = (a) => { 8 | switch(a.constructor){ 9 | case Number: 10 | return absNum(a); 11 | case String: 12 | return absString(a); 13 | case Array: 14 | return absArray(a); 15 | case Complex: 16 | return absComplex(a); 17 | } 18 | }; 19 | 20 | const absNum = (a) => { 21 | return Math.abs(a); 22 | }; 23 | 24 | const absString = (a) => { 25 | let absolute = Math.abs(Number(a)); 26 | return absolute.toString(10); 27 | }; 28 | 29 | //Determinant of a matrix 30 | 31 | function det(a,n){ 32 | let determinant = 0, c1, c2, sign = 1; 33 | let detArr = [...Array(n)].map(x => Array(n).fill(0)); 34 | if(n === 1){ 35 | return a[0][0]; 36 | } 37 | else if(n === 2){ 38 | determinant = a[0][0]*a[1][1]- a[0][1]*a[1][0]; 39 | return determinant; 40 | } 41 | else{ 42 | for(let i = 0; i < n; i++){ 43 | c1 = 0, c2 = 0; 44 | for(let j = 0; j < n; j++){ 45 | 46 | for(let k = 0; k < n; k++){ 47 | if(j != 0 && k != i){ 48 | detArr[c1][c2] = a[j][k]; 49 | c2++; 50 | if(c2 > n - 2){ 51 | c1++; 52 | c2 = 0; 53 | } 54 | } 55 | } 56 | } 57 | determinant = determinant + sign*a[0][i]* det(detArr, n - 1); 58 | sign = -1*sign; 59 | } 60 | return determinant; 61 | } 62 | } 63 | 64 | 65 | const absArray = (a) => { 66 | if(a[0].length === undefined){ 67 | return 0; 68 | } 69 | 70 | if(a.length !== a[0].length){ 71 | throw new TypeError(`Invalid argument received: ${JSON.stringify(a)}\n'abs()' only accepts array with similar dimensions!\n`); 72 | } 73 | return Math.abs(det(a, a.length)); 74 | }; 75 | 76 | const absComplex = (a) => { 77 | return parseFloat(Math.sqrt(Math.pow(a.re , 2) + Math.pow(a.im , 2)).toFixed(2)); 78 | }; 79 | 80 | module.exports = (...args) => { 81 | if(args.length !== 1){ 82 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n'abs()' only accepts only one parameter!\n`); 83 | } 84 | validate(args); 85 | return abs(args[0]); 86 | }; 87 | -------------------------------------------------------------------------------- /src/add/index.js: -------------------------------------------------------------------------------- 1 | /*Addition for all datatypes and data-structures 2 | *Function: M.add(...args) 3 | */ 4 | const Complex = require('../complex'); 5 | const validate = require('../validation/argument-length'); 6 | 7 | const add = (...args) => { 8 | switch(args[0].constructor){ 9 | case Number: 10 | return addNum(...args); 11 | case String: 12 | return addString(...args); 13 | case Array: 14 | if(args[0][0].constructor === Array){ 15 | return args.reduce((prev, next) => 16 | addMatrix(prev, next)); 17 | } 18 | else{ 19 | return args.reduce((prev, next) => 20 | addArray(prev, next)); 21 | } 22 | case Complex: 23 | return addComplex(...args); 24 | } 25 | }; 26 | 27 | const addNum = (...args) => { 28 | let sum = 0; 29 | args.forEach((e) => { 30 | sum += e; 31 | }); 32 | return sum; 33 | }; 34 | 35 | const addString = (...args) => { 36 | let sum = 0; 37 | args.forEach((e) => { 38 | sum += Number(e); 39 | }); 40 | sum = sum.toString(10); 41 | return sum; 42 | }; 43 | 44 | const addArray = (a,b) => { 45 | let sum = new Array(b.length); 46 | for(let i = 0; i < b.length; i++){ 47 | let l = a[i] + b[i]; 48 | sum.push(l); 49 | sum = sum.filter(n => n); 50 | } 51 | return sum; 52 | }; 53 | 54 | const addMatrix = (a,b) => { 55 | let sum = [...Array(b.length)].map(x => Array(b[0].length).fill(0)); 56 | for(let i = 0; i < b.length; i++){ 57 | for(let j = 0; j < b[0].length; j++){ 58 | 59 | sum[i][j] = a[i][j] + b[i][j]; 60 | } 61 | } 62 | return sum; 63 | }; 64 | 65 | const addComplex = (...args) => { 66 | let sum = { 67 | re: 0, 68 | im: 0 69 | }; 70 | args.forEach((e) => { 71 | sum.re += e.re; 72 | sum.im += e.im; 73 | }); 74 | return sum; 75 | }; 76 | 77 | module.exports = (...args) => { 78 | if(args.length <= 1){ 79 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n 'add()' only accepts 2 or more parameters!\n`); 80 | } 81 | else { 82 | validate(args); 83 | } 84 | return add(...args); 85 | }; 86 | -------------------------------------------------------------------------------- /src/armstrong/index.js: -------------------------------------------------------------------------------- 1 | /* Armstrong Number 2 | * Function: isArmstrong() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | exports.check = num => { 8 | validate(num, 'isArmstrong'); 9 | let str = num.toString(); 10 | return num == str.split('').reduce((prev, next) => prev + Math.pow(parseInt(next, 10), str.length), 0); 11 | }; 12 | -------------------------------------------------------------------------------- /src/automorphic/index.js: -------------------------------------------------------------------------------- 1 | /* Automorphic Number 2 | * Function: isAutomorphic() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | exports.check = num => { 8 | validate(num, 'isAutomorphic'); 9 | let str = Math.pow(num, 2).toString(); 10 | return num == str.slice(str.indexOf(num.toString())); 11 | }; 12 | -------------------------------------------------------------------------------- /src/average/index.js: -------------------------------------------------------------------------------- 1 | /* Function: avg() */ 2 | 3 | const validate = require('../validation/number-array'); 4 | 5 | module.exports = arr => { 6 | validate(arr, 'avg'); 7 | let sum = arr.reduce((prev, next) => prev + next, 0); 8 | return Number((sum/arr.length).toFixed(5)); 9 | }; 10 | -------------------------------------------------------------------------------- /src/bell/index.js: -------------------------------------------------------------------------------- 1 | /* N-th bell Number 2 | * Function: bell() 3 | */ 4 | 5 | const validate = require('../validation/non-negative-integer'); 6 | 7 | function bell(number){ 8 | 9 | let bell = [...Array(number+1)].map(x => Array(number+1).fill(0)); 10 | 11 | bell[0][0] = 1; 12 | 13 | for( let i=1; i<=number; i++ ){ 14 | let temp = bell[i-1][i-1]; 15 | bell[i][0] = temp; 16 | 17 | for( let j=1; j<=i; j++ ){ 18 | let t1 = bell[i-1][j-1]; 19 | let t2 = bell[i][j-1]; 20 | bell[i][j] = t1 + t2; 21 | 22 | } 23 | 24 | } 25 | let v = bell[number][0]; 26 | return v; 27 | 28 | } 29 | 30 | exports.find = num => { 31 | 32 | validate(num, 'bell'); 33 | 34 | return bell(num); 35 | 36 | }; 37 | -------------------------------------------------------------------------------- /src/binomial-coeff/index.js: -------------------------------------------------------------------------------- 1 | /* nCr 2 | * Function: nCr() 3 | */ 4 | 5 | const validate = require('../validation/non-negative-integer'); 6 | 7 | /** 8 | * 9 | * @param {number, number} number 10 | * @returns {number} 11 | */ 12 | 13 | function nCr(number1, number2) { 14 | 15 | let res = 1; 16 | let i = 0; 17 | if(number2>number1-number2){ 18 | number2=number1-number2; 19 | } 20 | for(; i { 28 | validate(number1, 'nCr'); 29 | validate(number2, 'nCr'); 30 | if(number1 < number2){ 31 | throw new TypeError( 32 | `n >= r condition not satisfied.\n` 33 | ); 34 | } 35 | return nCr(number1, number2); 36 | }; 37 | -------------------------------------------------------------------------------- /src/carmichael/index.js: -------------------------------------------------------------------------------- 1 | /* Carmichael Number 2 | * Function: isCarmichael() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | const prime = require('../prime').check; 7 | 8 | function squarefree(i){ 9 | for(let k = 2; (k <= Math.sqrt(i) && i % k !== 0 && i/k % k !== 0); k++){ 10 | return true; 11 | } 12 | } 13 | 14 | function isCarmichael(n){ 15 | let c = 0; 16 | for(let i = 1; i <= n; i++){ 17 | if(n % i == 0){ 18 | if(prime(i) && ((n-1) % (i-1) == 0)){ 19 | c++; 20 | if(c >= 3 && i % 2 != 0 && squarefree(i)){ 21 | return true; 22 | 23 | } 24 | } 25 | } 26 | } 27 | return false; 28 | } 29 | exports.check = n => { 30 | validate(n, 'isCarmichael'); 31 | return isCarmichael(n); 32 | }; 33 | -------------------------------------------------------------------------------- /src/catalan/index.js: -------------------------------------------------------------------------------- 1 | /* Nth Catalan Number 2 | * Function: catalan() 3 | */ 4 | 5 | const validate = require('../validation/non-negative-integer'), 6 | nCr = require('../binomial-coeff/index'); 7 | 8 | exports.find = num => { 9 | validate(num, 'catalan'); 10 | let c = nCr(2 * num, num); 11 | return c / (num + 1); 12 | }; 13 | -------------------------------------------------------------------------------- /src/check/index.js: -------------------------------------------------------------------------------- 1 | /*Function: check()*/ 2 | 3 | const validate = require('../validation/string'); 4 | 5 | module.exports = arg => { 6 | validate(arg, 'check'); 7 | const number = require(`../${arg}`); 8 | if('check' in number){ 9 | return number.check; 10 | } 11 | else{ 12 | throw new TypeError(`Invalid argument received!`); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /src/complex/index.js: -------------------------------------------------------------------------------- 1 | /*Complex Numbers 2 | * Use: const a = new M.Complex(real, imaginery); 3 | */ 4 | const validate = require('../validation/integer-object'); 5 | class Complex { 6 | constructor(r,i) { 7 | this.r = r; 8 | this.i = i; 9 | validate(this.r, 'Complex'); 10 | validate(this.i, 'Complex'); 11 | } 12 | get re() { 13 | return this.r; 14 | } 15 | get im(){ 16 | return this.i; 17 | } 18 | abs(){ 19 | return parseFloat(Math.sqrt(Math.pow(this.r , 2) + Math.pow(this.i , 2)).toFixed(2)); 20 | } 21 | conjugate(){ 22 | let img = -this.i; 23 | const obj = new Complex(this.r, img); 24 | return obj; 25 | } 26 | get conj(){ 27 | return this.conjugate(); 28 | } 29 | add(args){ 30 | validate(args, 'add'); 31 | if(Number.isInteger(args)){ 32 | let obj = new Complex(this.r + args, this.i); 33 | return obj; 34 | } 35 | else{ 36 | let obj = new Complex(this.r + args.r, this.i + args.i); 37 | return obj; 38 | } 39 | } 40 | sub(args){ 41 | validate(args, 'sub'); 42 | if(Number.isInteger(args)){ 43 | let obj = new Complex(this.r - args, this.i); 44 | return obj; 45 | } 46 | else{ 47 | let obj = new Complex(this.r - args.r, this.i - args.i); 48 | return obj; 49 | } 50 | } 51 | multiply(args){ 52 | validate(args, 'multiply'); 53 | if(Number.isInteger(args)){ 54 | let obj = new Complex(this.r * args, this.i * args); 55 | return obj; 56 | } 57 | else{ 58 | let obj = new Complex((this.r * args.r - this.i * args.i), 59 | ( this.r * args.i + args.r * this.i)); 60 | return obj; 61 | } 62 | } 63 | divide(args){ 64 | validate(args, 'divide'); 65 | if(Number.isInteger(args)){ 66 | let obj = new Complex(parseFloat(this.r/args), 67 | parseFloat(this.i/args)); 68 | return obj; 69 | } 70 | else { 71 | let dm = Math.pow(args.r, 2) + Math.pow(args.i , 2); 72 | let real = parseFloat(((this.r * args.r + this.i * args.im)/dm).toFixed(2)); 73 | let img = parseFloat(((args.r * this.i - this.r * args.i)/dm).toFixed(2)); 74 | let obj = new Complex(real, img); 75 | return obj; 76 | } 77 | 78 | } 79 | } 80 | module.exports = Complex; 81 | -------------------------------------------------------------------------------- /src/convert/index.js: -------------------------------------------------------------------------------- 1 | //Include your validation cases here 2 | const validateNumber = require("../validation/number"), 3 | validateNonNegative = require("../validation/non-negative-integer"); 4 | 5 | /* 6 | * Perform the necessary validation within the function and not in the exports block. 7 | */ 8 | 9 | //Function to round-off the variable, with fixed length after the decimal 10 | let round = (value, length) => { 11 | 12 | validateNonNegative(length, "convert"); 13 | if(length >= 1){ 14 | let temp = Number.parseFloat(value).toFixed(length); 15 | return Number.parseFloat(temp); 16 | } 17 | else{ 18 | return value; 19 | } 20 | }; 21 | 22 | //Convert degrees to radians 23 | let toRadians = num => { 24 | validateNumber(num, 'convert'); 25 | 26 | return num*Math.PI/180; 27 | }; 28 | 29 | //Convert radians to degrees 30 | let toDegrees = num => { 31 | validateNumber(num, 'convert'); 32 | 33 | return num*180/Math.PI; 34 | }; 35 | 36 | let convert = (num, object, trim = 0) => { 37 | 38 | //Verification if the object variable is of type 'object' 39 | if(object.constructor !== Object){ 40 | throw new TypeError(`Invalid argument received: ${JSON.stringify(object)} in convert() must be an object!\n`); 41 | } 42 | 43 | //Check if the object variable has the required fields 44 | if(object.constructor === Object && (!(object.hasOwnProperty('from')) || !(object.hasOwnProperty('to')))){ 45 | throw new ReferenceError(`Invalid argument received: ${JSON.stringify(object)} in convert() must contain to and from fields!\n`); 46 | } 47 | 48 | //Check if the value of trim is valid or not 49 | validateNonNegative(trim, "convert"); 50 | 51 | //Store the output of your function here 52 | let value; 53 | 54 | //Handle your to and from cases here 55 | if(object.from === 'radian' && object.to === 'degree'){ 56 | value = round(toDegrees(num), trim); 57 | } 58 | else if(object.from === 'degree' && object.to === 'radian'){ 59 | value = round(toRadians(num), trim); 60 | } 61 | 62 | //default error message 63 | else{ 64 | value = "Your to/from parameter was not found!"; 65 | } 66 | 67 | //Returning the answer 68 | return value; 69 | }; 70 | 71 | //Exporting the convert function 72 | module.exports = convert; 73 | -------------------------------------------------------------------------------- /src/count/index.js: -------------------------------------------------------------------------------- 1 | const validate = require('../validation/count'); 2 | 3 | const deepCompare = (var1, var2) => { 4 | if((typeof var1 === "string" || typeof var1 === "number")&&(typeof var2 === "string" || typeof var2 === "number")){ 5 | return var1 === var2; 6 | } 7 | else if(Array.isArray(var1) && Array.isArray(var2)){ 8 | return (var1.filter(val => var2.includes(val)).length === var1.length && var1.length === var2.length); 9 | } 10 | else{ 11 | for(var ele in var1){ 12 | if(var1.hasOwnProperty(ele) !== var2.hasOwnProperty(ele)){ 13 | return false; 14 | } 15 | else{ 16 | if (!deepCompare(var1[ele], var2[ele])){ 17 | return false; 18 | } 19 | } 20 | } 21 | } 22 | return true; 23 | }; 24 | 25 | /** 26 | * Find the occurance count of a phrase 27 | * @param {string} str 28 | * @param {string} occurance 29 | */ 30 | const findStroccurance = (str, occurance) => { 31 | let count = 0; 32 | let flag = -1; 33 | 34 | do { 35 | flag = str.indexOf(occurance, flag + 1); 36 | if (flag !== -1) { 37 | count++; 38 | } 39 | } while (flag !== -1); 40 | 41 | return count; 42 | }; 43 | 44 | /** 45 | * Find the occurance count of a phrase 46 | * @param {object} obj 47 | * @param {string|number|object} occurance 48 | */ 49 | const findObjOccurance = (obj, occurance) => { 50 | let count = 0; 51 | let keyVals = Object.keys(obj); 52 | 53 | for(let i = 0; i < keyVals.length; i++){ 54 | if(deepCompare(occurance, obj[keyVals[i]])){ //obj[keyVals[i]] === occurance 55 | count++; 56 | } 57 | } 58 | return count; 59 | }; 60 | 61 | /** 62 | * Frequency 63 | * 64 | * `count()` accepts two arguments and return the occurance of second argument in first 65 | * @param {string|number|string[]|number[]|object} arg - Accepts either a String or an Array 66 | * @param {string|number|object} occurance - The phrase or array item you want to find 67 | */ 68 | module.exports = (...args) => { 69 | if(args.length > 2){ 70 | throw new TypeError(`Invalid input received! 'count()' does 71 | not accept more than 2 parameters!`); 72 | } 73 | let arg = args[0], occurance = args[1]; 74 | validate(arg, 'count'); 75 | 76 | if (arg && arg.constructor === Array) { 77 | return (arg = arg.filter(val => val === occurance).length); 78 | } 79 | else if(arg.constructor === Object){ 80 | return findObjOccurance(arg, occurance); 81 | } 82 | else { 83 | return findStroccurance(arg, occurance); 84 | } 85 | }; 86 | -------------------------------------------------------------------------------- /src/deficient/index.js: -------------------------------------------------------------------------------- 1 | const validate = require('../validation/positive-integer'); 2 | 3 | /** 4 | * Find all divisors of a positive number and return an array of the same 5 | */ 6 | const findDivisors = num => { 7 | let divisorsArr = [0]; 8 | 9 | if (num === 1) { 10 | divisorsArr = [0]; 11 | } else { 12 | for (let i = 1; i < num - 1; i++) { 13 | if (num % i === 0) { 14 | divisorsArr.push(i); 15 | } 16 | } 17 | } 18 | 19 | return divisorsArr; 20 | }; 21 | 22 | /** 23 | * add all divisors 24 | */ 25 | const addAllDivisors = arr => { 26 | const add = (total, num) => total + num; 27 | return arr.reduce(add); 28 | }; 29 | 30 | exports.check = num => { 31 | validate(num, 'deficient'); 32 | let divisors = findDivisors(num); 33 | let sum = addAllDivisors(divisors); 34 | 35 | return sum < num ? true : false; 36 | }; 37 | 38 | // console.log(d(10)); 39 | -------------------------------------------------------------------------------- /src/even/index.js: -------------------------------------------------------------------------------- 1 | /* Function: isEven() */ 2 | 3 | const validate = require('../validation/integer'); 4 | 5 | exports.check = num => { 6 | validate(num, 'isEven'); 7 | return num & 1 ? false : true; 8 | }; 9 | -------------------------------------------------------------------------------- /src/factorial/index.js: -------------------------------------------------------------------------------- 1 | /* Factorial of a Number 2 | * Function: factorial() 3 | */ 4 | 5 | const validate = require('../validation/non-negative-integer'); 6 | 7 | /** 8 | * 9 | * @param {number} number 10 | * @returns {number} 11 | */ 12 | function factorial(number) { 13 | let i = 1, 14 | result = 1; 15 | for (; i <= number; i++) { 16 | result = result * i; 17 | } 18 | return result; 19 | } 20 | 21 | exports.find = num => { 22 | validate(num, 'factorial'); 23 | return factorial(num); 24 | }; 25 | -------------------------------------------------------------------------------- /src/fibbinary/index.js: -------------------------------------------------------------------------------- 1 | const validate = require('../validation/positive-integer'); 2 | 3 | function isFibbinary(num){ 4 | if ((num & (num >> 1)) === 0) { 5 | return true; 6 | } 7 | 8 | else{ 9 | return false; 10 | } 11 | } 12 | 13 | exports.check = num => { 14 | validate(num, 'fibbinary'); 15 | return isFibbinary(num); 16 | }; -------------------------------------------------------------------------------- /src/fibonacci/index.js: -------------------------------------------------------------------------------- 1 | /* Fibonacci Number 2 | * Function: fibonacci() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'), 6 | matrixExpo = require('../matrixExponentiation').matrixExpo; 7 | 8 | 9 | function fib(num) { 10 | if (num <= 2) { 11 | return 1; 12 | } 13 | let F = [ 14 | [1,1], 15 | [1,0] 16 | ]; 17 | 18 | F = matrixExpo(F, num-1); 19 | return F[0][0]; 20 | } 21 | 22 | exports.find = num => { 23 | validate(num, 'fibonacci'); 24 | return fib(num); 25 | }; 26 | 27 | /* Range Fibonacci Number 28 | * Function: rangeFibonacci() 29 | */ 30 | 31 | const rangeFib = num => { 32 | let n1 = 0, n2 = 1, nextT = null, rangeArr = []; 33 | 34 | do { 35 | rangeArr.push(n1); 36 | nextT = n1 + n2; 37 | n1 = n2; 38 | n2 = nextT; 39 | } while (n1 <= num); 40 | return rangeArr; 41 | }; 42 | 43 | exports.range = num => { 44 | validate(num, 'rangeFibonacci'); 45 | return rangeFib(num); 46 | }; 47 | -------------------------------------------------------------------------------- /src/find/index.js: -------------------------------------------------------------------------------- 1 | /* Function : find() */ 2 | 3 | const validate = require('../validation/string'); 4 | 5 | module.exports = arg => { 6 | validate(arg,'find'); 7 | const number = require(`../${arg}`); 8 | if('find' in number){ 9 | return require(`../${arg}`).find; 10 | } 11 | else{ 12 | throw new TypeError(`Invalid argument received!`); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /src/frequency/index.js: -------------------------------------------------------------------------------- 1 | const validate = require('../validation/frequency'); 2 | 3 | /** 4 | * Find the occurence count of a phrase 5 | * @param {string} str 6 | * @param {string} occurance 7 | */ 8 | const findStrOccurence = (str, occurance) => { 9 | let count = 0; 10 | let flag = -1; 11 | 12 | do { 13 | flag = str.indexOf(occurance, flag + 1); 14 | if (flag !== -1) { 15 | count++; 16 | } 17 | } while (flag !== -1); 18 | 19 | return count; 20 | }; 21 | 22 | 23 | /** 24 | * Frequency 25 | * 26 | * `frequency()` accepts two arguments and return the occurance of second argument in first 27 | * @param {string|number|string[]|number[]} arg - Accepts either a String or an Array 28 | * @param {string|number} occurence - The phrase or array item you want to find 29 | */ 30 | module.exports = (arg, occurence) => { 31 | validate(arg, 'frequency'); 32 | 33 | if (arg && arg.constructor === Array) { 34 | return (arg = arg.filter(val => val === occurence).length); 35 | } else { 36 | return findStrOccurence(arg, occurence); 37 | } 38 | }; -------------------------------------------------------------------------------- /src/frugal/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | Frugal Numbers tester 3 | function name: isFrugal() 4 | */ 5 | 6 | const validate = require('../validation/positive-integer'); 7 | 8 | function genPrimeList(inp){ 9 | 10 | let list = []; 11 | let count = 0 ,temp = inp; 12 | 13 | //testing for all multiples of 2 14 | while(temp%2 === 0) 15 | { 16 | count++; 17 | temp = temp / 2; 18 | } 19 | 20 | if(count > 0){ 21 | list.push({num: 2, occurance: count}); 22 | } 23 | 24 | //for the remaining odd numbers. Note that prime numbers will remove their non-prime multiples BEFORE any 25 | //non- prime number can appear 26 | for(let i = 3; i < temp/2 ; i +=2 ){ 27 | count = 0; 28 | while(inp%i === 0) 29 | { 30 | count++; 31 | inp = inp / i; 32 | } 33 | if(count > 0){ 34 | list.push({num: i, occurance: count}); 35 | } 36 | } 37 | return list; 38 | } 39 | 40 | exports.check = inp => { 41 | validate(inp, 'isFrugal'); 42 | 43 | let primeFactorisation = genPrimeList(inp); 44 | let factorsLength = 0; 45 | let inpLength = inp.toString().length; 46 | 47 | for(let i = 0; i < primeFactorisation.length; i++){ 48 | 49 | factorsLength += primeFactorisation[i].num.toString().length; 50 | 51 | if(primeFactorisation[i].occurance > 1){ 52 | factorsLength += primeFactorisation[i].occurance.toString().length; 53 | } 54 | } 55 | 56 | return ((inpLength - factorsLength) > 0)? true: false; 57 | }; 58 | -------------------------------------------------------------------------------- /src/gcd/index.js: -------------------------------------------------------------------------------- 1 | /* Function: gcd() */ 2 | 3 | const validate = require('../validation/integer-array'); 4 | 5 | function gcd(a, b) { 6 | return (a == 0) ? b : gcd(b % a, a); 7 | } 8 | 9 | module.exports = arr => { 10 | validate(arr, 'gcd'); 11 | return arr.reduce((prev, next) => gcd(prev, next), arr[0]); 12 | }; 13 | -------------------------------------------------------------------------------- /src/graph/index.js: -------------------------------------------------------------------------------- 1 | class Graph{ 2 | constructor(vertices){ 3 | this.noOfVertices = vertices; 4 | this.Adjlist = new Map(); 5 | } 6 | 7 | addVertex(vertexData){ 8 | return this.Adjlist.set(vertexData, []); 9 | } 10 | 11 | addEdge(source, destination){ 12 | return this.Adjlist.get(source).push(destination); 13 | } 14 | 15 | printGraph(){ 16 | return this.Adjlist; 17 | } 18 | 19 | removeVertex(vertexName){ 20 | if(this.Adjlist.size === 0) 21 | { 22 | return "Empty Graph"; 23 | } 24 | else{ 25 | return this.Adjlist.delete(vertexName); 26 | } 27 | } 28 | 29 | removeEdge(vertexName, edgeName){ 30 | 31 | if(this.Adjlist.size === 0) 32 | { 33 | return "Empty Graph"; 34 | } 35 | 36 | if(!this.Adjlist.get(vertexName)) 37 | { 38 | return "Vertex does not exist in the Graph"; 39 | } 40 | 41 | let location = this.Adjlist.get(vertexName).indexOf(edgeName); 42 | 43 | if(location === -1){ 44 | return "Edge not found!"; 45 | } 46 | else{ 47 | return this.Adjlist.get(vertexName).splice(location, 1).toString(); 48 | } 49 | 50 | } 51 | 52 | bfs(startNode){ 53 | 54 | if(!Array.from(this.Adjlist.keys()).includes(startNode)){ 55 | return "Node doesn't exist in the Graph"; 56 | } 57 | let visited = []; 58 | let output = new Array(); 59 | 60 | for(let i = 0; i < this.noOfVertices; i++){ 61 | visited.push(false); 62 | } 63 | let queue = []; 64 | 65 | visited[startNode] = true; 66 | queue.push(startNode); 67 | 68 | while(queue.length > 0){ 69 | let getQueueElement = queue.shift(); 70 | output.push(getQueueElement); 71 | 72 | let getList = this.Adjlist.get(getQueueElement); 73 | 74 | for(let i = 0; i < getList.length; i++){ 75 | 76 | let neighbour = getList[i]; 77 | 78 | if(!visited[neighbour]){ 79 | 80 | visited[neighbour] = true; 81 | queue.push(neighbour); 82 | } 83 | } 84 | } 85 | return output; 86 | } 87 | 88 | dfs(startNode){ 89 | 90 | if(!Array.from(this.Adjlist.keys()).includes(startNode)){ 91 | return "Node doesn't exist in the Graph"; 92 | } 93 | let visited = []; 94 | let output = new Array(); 95 | 96 | for(let i = 0; i < this.noOfVertices; i++){ 97 | visited.push(false); 98 | } 99 | this.dfsUtil(startNode, visited, output); 100 | return output; 101 | } 102 | 103 | dfsUtil(node, visited, output){ 104 | visited[Array.from(this.Adjlist.keys()).indexOf(node)] = true; 105 | output.push(node); 106 | 107 | let neighbours = this.Adjlist.get(node); 108 | for(let i = 0; i < neighbours.length; i++){ 109 | if(visited[Array.from(this.Adjlist.keys()).indexOf(neighbours[i])] === false){ 110 | this.dfsUtil(neighbours[i], visited, output); 111 | } 112 | } 113 | } 114 | } 115 | 116 | module.exports = Graph; -------------------------------------------------------------------------------- /src/harshad/index.js: -------------------------------------------------------------------------------- 1 | /* Harshad Number 2 | * Function: isHarshad() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | function isHarshad(num){ 8 | let sum = 0; 9 | for(let i=num;i>0;i=parseInt(i/10,10)){ 10 | sum+=i%10; 11 | } 12 | return num%sum === 0; 13 | } 14 | exports.check = num => { 15 | validate(num, 'isHarshad'); 16 | return isHarshad(num); 17 | }; 18 | -------------------------------------------------------------------------------- /src/hexagonal/index.js: -------------------------------------------------------------------------------- 1 | /* Hexagonal Number 2 | * Function: nth hexagonal number() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | function hex(num) 8 | { 9 | return num * (2 * num - 1); 10 | } 11 | 12 | exports.find = num => { 13 | validate(num, 'hexagonal'); 14 | return hex(num); 15 | }; 16 | -------------------------------------------------------------------------------- /src/hoax/index.js: -------------------------------------------------------------------------------- 1 | const validate = require('../validation/positive-integer'); 2 | const check = require('../check'); 3 | /** 4 | * hoax Number 5 | */ 6 | 7 | /** 8 | * find distinct prime factors 9 | */ 10 | const findPrimeFactors = (num) => { 11 | const primeFactorArr = []; 12 | 13 | if (num % 2 === 0) { 14 | while (num % 2 === 0) { 15 | num /= 2; 16 | } 17 | primeFactorArr.push(2); 18 | } 19 | 20 | for (let i = 3; i <= Math.sqrt(num); i += 2) { 21 | // Check if i is prime factor 22 | if (num % i === 0) { 23 | while (num % i === 0) { 24 | num /= i; 25 | } 26 | primeFactorArr.push(i); 27 | } 28 | } 29 | 30 | if (num > 2) { 31 | primeFactorArr.push(num); 32 | } 33 | return primeFactorArr; 34 | }; 35 | 36 | /** 37 | * check if the number is a hoax 38 | */ 39 | const isHoaxNumber = (num) => { 40 | 41 | if(check('prime')(num) === true){ 42 | return false; 43 | } 44 | else{ 45 | num = num.toString(); 46 | const primeFactor = findPrimeFactors(num); 47 | const sumofNum = num 48 | .split('') 49 | .map(val => Number(val)) 50 | .reduce((a, b) => a + b); 51 | 52 | let sumOfPrimeFactor = 0; 53 | for(let i = 0; i < primeFactor.length; i++){ 54 | let sum = 0; 55 | let pf = primeFactor[i]; 56 | while(pf > 0){ 57 | sum += pf % 10; 58 | pf = parseInt((pf / 10),10); 59 | } 60 | sumOfPrimeFactor += sum; 61 | } 62 | 63 | return sumofNum === sumOfPrimeFactor; 64 | } 65 | }; 66 | 67 | exports.check = (num) => { 68 | validate(num); 69 | return isHoaxNumber(num); 70 | }; 71 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | /* Numbers' Problem */ 2 | const Complex = require('./complex'), 3 | check = require('./check'), 4 | find = require('./find'), 5 | range = require('./range'); 6 | 7 | /* Helper Functions */ 8 | const perf = require('./performance'), 9 | pipe = require('./pipe'); 10 | 11 | /* Core Tools */ 12 | const abs = require('./absolute'), 13 | add = require('./add'), 14 | avg = require('./average'), 15 | convert = require('./convert'), 16 | nCr = require('./binomial-coeff'), 17 | count = require('./count'), 18 | div = require('./divide'), 19 | gcd = require('./gcd'), 20 | lcm = require('./lcm'), 21 | length = require('./length'), 22 | max = require('./max'), 23 | median = require('./median'), 24 | min = require('./min'), 25 | inverse = require('./inverse'), 26 | mul = require('./multiply'), 27 | permute = require('./permute'), 28 | phi = require('./totient'), 29 | popcount = require('./popcount'), 30 | pow = require('./pow'), 31 | sort = require('./sort'), 32 | sub = require('./subtract'), 33 | sum = require('./sum'), 34 | union = require('./union'); 35 | 36 | /* Data Structures */ 37 | const Matrix = require('./Matrix'), 38 | BinarySearchTree = require('./binary-search-tree'), 39 | Graph = require('./graph'), 40 | PriorityQueue = require('./priority-queue'), 41 | Stack = require('./stack'); 42 | 43 | /* M - Mathball Object */ 44 | module.exports = { 45 | find, 46 | permute, 47 | gcd, 48 | add, 49 | sub, 50 | mul, 51 | div, 52 | abs, 53 | lcm, 54 | length, 55 | /* ===== */ 56 | /* Replace these two with 'Matrix' */ 57 | Matrix, 58 | /* ===== */ 59 | max, 60 | median, 61 | min, 62 | nCr, 63 | perf, 64 | pipe, 65 | popcount, 66 | pow, 67 | phi, 68 | sort, 69 | sum, 70 | avg, 71 | inverse, 72 | count, 73 | union, 74 | BinarySearchTree, 75 | PriorityQueue, 76 | Stack, 77 | Graph, 78 | range, 79 | convert, 80 | Complex, 81 | check 82 | }; 83 | -------------------------------------------------------------------------------- /src/inverse/index.js: -------------------------------------------------------------------------------- 1 | /* Modular Multiplicative Inverse 2 | * function : modInv() 3 | */ 4 | 5 | const validate = require('../validation/integer'); 6 | 7 | const gcd = require('../gcd'); 8 | 9 | function modInv(a , m) { 10 | let arr = [a,m]; 11 | if(gcd(arr) == 1) { 12 | for(var i = 1; i < m ; i++) 13 | if((a * i) % m == 1) 14 | return i; 15 | } 16 | else 17 | return 0; 18 | } 19 | 20 | module.exports = (a,m) => { 21 | validate(a , 'modInv'); 22 | validate(m, 'modInv'); 23 | return modInv(a,m); 24 | }; 25 | -------------------------------------------------------------------------------- /src/kaprekar/index.js: -------------------------------------------------------------------------------- 1 | /* Kaprekar Number 2 | * Function: isKaprekar() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | exports.check = num => { 8 | validate(num, 'isKaprekar'); 9 | if (num == 1) { 10 | return true; 11 | } 12 | var sqNum = num * num; 13 | var countDigits = num.toString().length; 14 | var rDigits = 0; 15 | while (rDigits < countDigits) { 16 | rDigits++; 17 | var eqParts = Math.pow(10, rDigits); 18 | if (eqParts == num) { 19 | continue; 20 | } 21 | var sum = Math.floor(sqNum / eqParts) + (sqNum % eqParts); 22 | if (sum == num) { 23 | return true; 24 | } 25 | } 26 | return false; 27 | }; 28 | -------------------------------------------------------------------------------- /src/lcm/index.js: -------------------------------------------------------------------------------- 1 | /* Function: lcm() */ 2 | 3 | const validate = require('../validation/integer-array'); 4 | 5 | function gcd(a, b) { 6 | return (a == 0) ? b : gcd(b % a, a); 7 | } 8 | 9 | module.exports = arr => { 10 | validate(arr, 'lcm'); 11 | return arr.reduce((prev, next) => (prev * next) / gcd(prev, next), arr[0]); 12 | }; 13 | -------------------------------------------------------------------------------- /src/length/index.js: -------------------------------------------------------------------------------- 1 | /* Function: length() */ 2 | 3 | function validate(arg) { 4 | let types = ['number', 'object', 'string']; 5 | if(types.indexOf(typeof arg) == -1 || String(arg) == 'null' || 6 | (typeof arg == 'number' && (arg + 1 == arg || arg != arg))) { 7 | throw new TypeError(`Invalid argument received: ${JSON.stringify(arg)}\n'length()' only accept either a real number, string, object or array!\n`); 8 | } 9 | } 10 | 11 | module.exports = item => { 12 | validate(item); 13 | switch(item.constructor) { 14 | case Number: 15 | if(Number.isInteger(item)) return item.toString().length; 16 | else { 17 | let arr = item.toString().split(''); 18 | arr.splice(arr.indexOf('.'), 1); 19 | return arr.length; 20 | } 21 | case String: 22 | case Array: 23 | return item.length; 24 | case Object: 25 | return Object.keys(item).length; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /src/lucky/index.js: -------------------------------------------------------------------------------- 1 | /* Lucky Number 2 | * Function: isLucky() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | exports.check = num => { 8 | validate(num, 'isLucky'); 9 | var counter = 2; 10 | while (true) { 11 | if (counter > num) { 12 | return true; 13 | } else if (num % counter === 0) { 14 | return false; 15 | } 16 | num = num - Math.floor(num / counter); 17 | counter++; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /src/magic/index.js: -------------------------------------------------------------------------------- 1 | /* Magic Number 2 | * Function: magic() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | exports.find = num => { 8 | validate(num, 'magic'); 9 | let pow = 1, res = 0; 10 | while (num) { 11 | pow *= 5; 12 | res += num & 1 ? pow : 0; 13 | num >>= 1; 14 | } 15 | return res; 16 | }; 17 | -------------------------------------------------------------------------------- /src/matrixChain/index.js: -------------------------------------------------------------------------------- 1 | /*Matrix Chain Multiplication 2 | *Function: M.matrixChain([]) 3 | */ 4 | 5 | const validate = require('../validation/integer-array'); 6 | 7 | const matrixChain = (ar) => { 8 | let n = ar.length; 9 | let m = [...Array(n)].map(x => Array(n).fill(0)); 10 | let cost, j; 11 | 12 | for(let i = 0; i < n; i++){ 13 | m[i][i] = 0; 14 | } 15 | for(let l = 2; l < n; l++){ 16 | for(let i = 1; i < (n-l+1); i++){ 17 | j = i + l - 1; 18 | m[i][j] = Number.MAX_SAFE_INTEGER; 19 | for(let k = i; k <= j-1; k++){ 20 | cost = m[i][k] + m[k+1][j] + 21 | (ar[i-1]*ar[k]*ar[j]); 22 | if(cost < m[i][j]){ 23 | m[i][j] = cost;} 24 | } 25 | } 26 | } 27 | return m[1][n-1]; 28 | }; 29 | 30 | exports.matrixChain = (arr) => { 31 | validate(arr, 'matrixChain'); 32 | return matrixChain(arr); 33 | }; 34 | -------------------------------------------------------------------------------- /src/matrixExponentiation/index.js: -------------------------------------------------------------------------------- 1 | /*Fast way to implement matrix exponentiation 2 | Function: matrixExpo(matrix, power) 3 | */ 4 | 5 | const validate = require('../validation/number-array'), 6 | validateNumber = require('../validation/positive-integer'), 7 | multiply = require('../multiply'); 8 | 9 | function matrixExpo(matrix, power, dimension){ 10 | let i, j, answer = [], temp = []; 11 | 12 | 13 | //Generating identity matrix of order 'dimension' 14 | for(i = 0; i< dimension; i++){ 15 | for(j = 0; j< dimension; j++){ 16 | if(i == j){ 17 | temp.push(0); 18 | } 19 | else{ 20 | temp.push(1); 21 | } 22 | } 23 | answer.push(temp.reverse()); 24 | temp = []; 25 | } 26 | 27 | while(power > 0){ 28 | if(power%2 === 1){ 29 | answer = multiply(matrix, answer); 30 | } 31 | matrix = multiply(matrix, matrix); 32 | power = Math.floor(power/2); 33 | } 34 | 35 | return answer; 36 | } 37 | 38 | exports.matrixExpo = (mat1, power) => { 39 | 40 | validateNumber(power, "matrixExpo"); 41 | let dimension = mat1.length; 42 | 43 | for(let i = 0; i< dimension; i++){ 44 | 45 | validate(mat1[i], "matrixExpo"); 46 | 47 | if(mat1[i].length !== dimension){ 48 | return "Not a square matrix"; 49 | } 50 | } 51 | return matrixExpo(mat1, power, dimension); 52 | }; 53 | -------------------------------------------------------------------------------- /src/max/index.js: -------------------------------------------------------------------------------- 1 | /* Function: max() */ 2 | 3 | const validate = require('../validation/number-array'); 4 | 5 | module.exports = arr => { 6 | validate(arr, 'max'); 7 | return Math.max(...arr); 8 | }; 9 | -------------------------------------------------------------------------------- /src/median/index.js: -------------------------------------------------------------------------------- 1 | /* Function: median() */ 2 | 3 | const validate = require('../validation/number-array'); 4 | 5 | module.exports = arr => { 6 | validate(arr, 'median'); 7 | arr.sort(function(a,b){ 8 | return a-b; 9 | }); 10 | 11 | var half = Math.floor(arr.length / 2); 12 | 13 | if (arr.length % 2){ 14 | return arr[half]; 15 | } 16 | else{ 17 | return (arr[half - 1] + arr[half]) / 2.0; 18 | } 19 | 20 | }; 21 | -------------------------------------------------------------------------------- /src/min/index.js: -------------------------------------------------------------------------------- 1 | /* Function: min() */ 2 | 3 | const validate = require('../validation/number-array'); 4 | 5 | module.exports = arr => { 6 | validate(arr, 'min'); 7 | return Math.min(...arr); 8 | }; 9 | -------------------------------------------------------------------------------- /src/multiply/index.js: -------------------------------------------------------------------------------- 1 | /*Multiplication across all data-types and data-structures 2 | *Function: M.mul(m,n) 3 | */ 4 | const Complex = require('../complex'); 5 | const validate = require('../validation/argument-length'); 6 | 7 | const mul = (a,b) => { 8 | switch(a.constructor){ 9 | case Number: 10 | return mulNum(a,b); 11 | case String: 12 | return mulString(a,b); 13 | case Array: 14 | if(a[0].constructor === Array){ 15 | return mulMatrix(a,b); 16 | } 17 | else{ 18 | return mulArray(a,b); 19 | } 20 | case Complex: 21 | return mulComplex(a,b); 22 | } 23 | }; 24 | 25 | const mulNum = (a,b) => { 26 | return a * b; 27 | }; 28 | 29 | const mulString = (a,b) => { 30 | let pro = Number(a) * Number(b); 31 | return pro.toString(10); 32 | }; 33 | 34 | const mulArray = (a,b) => { 35 | let pro = new Array(a.length); 36 | for(let i = 0; i < a.length; i++){ 37 | let l = a[i] * b[i]; 38 | pro.push(l); 39 | pro = pro.filter(n => n); 40 | } 41 | return pro; 42 | }; 43 | 44 | const mulMatrix = (a,b) => { 45 | let pro = [...Array(b.length)].map(x => Array(b[0].length).fill(0)); 46 | for(let i = 0; i < a.length; i++){ 47 | for(let j = 0; j < b[0].length; j++){ 48 | let sum = 0; 49 | for(let k = 0; k < a[0].length; k++){ 50 | sum += a[i][k] * b[k][j]; 51 | } 52 | pro[i][j] = sum; 53 | } 54 | } 55 | return pro; 56 | }; 57 | 58 | const mulComplex = (a,b) => { 59 | let pro = { 60 | re: 0, 61 | im: 0 62 | }; 63 | pro.re = a.re * b.re; 64 | pro.im = a.im * b.im; 65 | return pro; 66 | }; 67 | 68 | module.exports = (...args) => { 69 | if(args.length > 2){ 70 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n'mul()' only accepts two parameters!\n`); 71 | } 72 | validate(args); 73 | return mul(args[0], args[1]); 74 | }; 75 | -------------------------------------------------------------------------------- /src/neon/index.js: -------------------------------------------------------------------------------- 1 | /* Neon Number 2 | * Function: isNeon() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | exports.check = num => { 8 | validate(num, 'isNeon'); 9 | return num == Math.pow(num, 2).toString().split('') 10 | .reduce((prev, next) => prev + parseInt(next, 10), 0); 11 | }; 12 | -------------------------------------------------------------------------------- /src/odd/index.js: -------------------------------------------------------------------------------- 1 | /* Function: isOdd() */ 2 | 3 | const validate = require('../validation/integer'); 4 | 5 | exports.check = num => { 6 | validate(num, 'isOdd'); 7 | return num & 1 ? true : false; 8 | }; 9 | -------------------------------------------------------------------------------- /src/padovan/index.js: -------------------------------------------------------------------------------- 1 | /* Padovan Number 2 | * Function: padovan() 3 | */ 4 | 5 | const validate = require("../validation/positive-integer"); 6 | 7 | function pad(num) { 8 | let pPrevPrev = 1, 9 | pPrev = 1, 10 | pCurr = 1, 11 | pNext = 1; 12 | for (let i = 3; i <= num; i++) { 13 | pNext = pPrevPrev + pPrev; 14 | pPrevPrev = pPrev; 15 | pPrev = pCurr; 16 | pCurr = pNext; 17 | } 18 | return pNext; 19 | } 20 | 21 | exports.find = num => { 22 | validate(num, "padovan"); 23 | return pad(num); 24 | }; 25 | -------------------------------------------------------------------------------- /src/palindrome/index.js: -------------------------------------------------------------------------------- 1 | /* Palindromic Number 2 | * Function: isPalindrome() 3 | * Function: rangePal() --> to return all palindromic numbers from 0 to end 4 | */ 5 | 6 | const validate = require('../validation/non-negative-integer'); 7 | 8 | exports.check = num => { 9 | validate(num, 'isPalindrome'); 10 | return num == num.toString().split('').reverse().join(''); 11 | }; 12 | 13 | exports.range = (start, end) => { 14 | validate(start, 'rangePal'); 15 | validate(end, 'rangePal'); 16 | 17 | if(start > end) 18 | { 19 | throw new TypeError( 20 | `Starting number must be smaller than the ending number!!\n` 21 | ); 22 | } 23 | let palinList = [], lim = 0; 24 | 25 | lim = (end < 10)? end : start; 26 | 27 | for(let i = start; i < lim; i++) 28 | { 29 | palinList.push(i); 30 | } 31 | 32 | for(let i = lim; i < end; i++) 33 | { 34 | if(i.toString() === i.toString().split('').reverse().join('')){ 35 | palinList.push(i); 36 | } 37 | } 38 | 39 | return palinList; 40 | }; 41 | -------------------------------------------------------------------------------- /src/perfect/index.js: -------------------------------------------------------------------------------- 1 | /* Perfect Number 2 | * Function: isPerfect() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | exports.check = num => { 8 | validate(num, 'isPerfect'); 9 | var count = 0; 10 | for(var i=1; i { 4 | console.time('\x1b[36mTime Taken\x1b[0m'); 5 | fn(...arg); 6 | console.timeEnd('\x1b[36mTime Taken\x1b[0m'); 7 | } 8 | -------------------------------------------------------------------------------- /src/permute/index.js: -------------------------------------------------------------------------------- 1 | /* String Permutation 2 | * Function: permute() 3 | */ 4 | 5 | const validate = require('../validation/string'); 6 | 7 | function permute(str) { 8 | let letters = str.split(''), 9 | results = [[letters.shift()]]; 10 | while (letters.length) { 11 | const currLetter = letters.shift(); 12 | let tmpResults = []; 13 | results.forEach(result => { 14 | let rIdx = 0; 15 | while (rIdx <= result.length) { 16 | const tmp = [...result]; 17 | tmp.splice(rIdx, 0, currLetter); 18 | tmpResults.push(tmp); 19 | rIdx++; 20 | } 21 | }); 22 | results = tmpResults; 23 | } 24 | return results 25 | .map(letterArray => letterArray.join('')) 26 | .filter((el, idx, self) => self.indexOf(el) === idx) 27 | .sort(); 28 | } 29 | 30 | module.exports = str => { 31 | validate(str, 'permutation'); 32 | return permute(str); 33 | }; 34 | -------------------------------------------------------------------------------- /src/pipe/index.js: -------------------------------------------------------------------------------- 1 | /* Function: pipe() */ 2 | const validate = require('../validation/pipe'), 3 | add = require('../add'), 4 | sub = require('../subtract'), 5 | mul = require('../multiply'), 6 | div = require('../divide'), 7 | abs = require('../absolute'), 8 | sum = require('../sum'); 9 | 10 | module.exports = arg => { 11 | validate(arg, 'pipe'); 12 | let cache = arg; 13 | let pipe = { 14 | add(...args) { 15 | cache = add(cache, ...args); 16 | return this; 17 | }, 18 | sub(...args) { 19 | cache = sub(cache, ...args); 20 | return this; 21 | }, 22 | mul(...args) { 23 | cache = mul(cache, ...args); 24 | return this; 25 | }, 26 | div(...args) { 27 | cache = div(cache, ...args); 28 | return this; 29 | }, 30 | abs() { 31 | cache = abs(cache); 32 | return this; 33 | }, 34 | sum() { 35 | cache = sum(cache); 36 | return this; 37 | }, 38 | done() { 39 | return cache; 40 | } 41 | }; 42 | return pipe; 43 | }; 44 | -------------------------------------------------------------------------------- /src/popcount/index.js: -------------------------------------------------------------------------------- 1 | /* Function: popcount() */ 2 | 3 | const validate = require('../validation/positive-integer'); 4 | 5 | function popcount(num) { 6 | let count = 0; 7 | while (num){ 8 | num &= (num-1); 9 | count++; 10 | } 11 | return count; 12 | } 13 | 14 | module.exports = num => { 15 | validate(num, 'popcount'); 16 | return popcount(num); 17 | }; 18 | -------------------------------------------------------------------------------- /src/pow/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | Function: pow(base,exp,modulo) 3 | */ 4 | 5 | const validate = require("../validation/non-negative-integer"); 6 | 7 | function pow(base, exp, modulo = Number.MAX_SAFE_INTEGER){ 8 | 9 | let result = 1; 10 | 11 | base = base % modulo; 12 | 13 | while (exp > 0) 14 | { 15 | if (exp%2 == 1) 16 | { 17 | result = (result*base) % modulo; 18 | } 19 | 20 | exp = (exp/2) | 0; 21 | 22 | base = (base*base) % modulo; 23 | } 24 | return result; 25 | } 26 | 27 | module.exports = (base,exp,modulo) => { 28 | 29 | validate(base, "pow"); 30 | validate(exp, "pow"); 31 | 32 | if( typeof modulo === "undefined" ){ 33 | return pow(base, exp); 34 | } 35 | 36 | else{ 37 | validate(modulo, "pow"); 38 | return pow(base, exp, modulo); 39 | } 40 | }; -------------------------------------------------------------------------------- /src/prime-factor/index.js: -------------------------------------------------------------------------------- 1 | /* Prime factors 2 | * Function: findPrimeFactors() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | function findPrimeFactors(num) { 8 | const result = []; 9 | while (num % 2 === 0) { 10 | result.push(2); 11 | num = num / 2; 12 | } 13 | 14 | for (let i = 3; i <= Math.sqrt(num); i = i + 2) { 15 | while (num % i == 0) { 16 | result.push(i); 17 | num = num / i; 18 | } 19 | } 20 | 21 | if (num > 2) { 22 | result.push(num); 23 | } 24 | 25 | return result; 26 | } 27 | 28 | exports.find = num => { 29 | validate(num, 'findPrimeFactors'); 30 | return findPrimeFactors(num); 31 | }; 32 | -------------------------------------------------------------------------------- /src/prime/index.js: -------------------------------------------------------------------------------- 1 | /* Prime Number 2 | * Function: isPrime() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | exports.check = num => { 8 | validate(num, 'isPrime'); 9 | if (num === 2 || num === 3) { return true; } 10 | if (num < 2 || num % 2 === 0) { return false; } 11 | if (num < 9) { return true; } 12 | if (num % 3 === 0) { return false; } 13 | let r = Math.sqrt(num); 14 | let f = 5; 15 | while (f <= r) { 16 | if (num % f === 0) { return false; } 17 | if (num % (f + 2) === 0) { return false; } 18 | f = f + 6; 19 | } 20 | return true; 21 | }; 22 | 23 | /* Range of Prime Numbers 24 | * Function: rangePrime() 25 | */ 26 | 27 | exports.range = (n) => { 28 | validate(n, 'rangePrime'); 29 | let numbers = Array(n + 1).fill(true); 30 | numbers[0] = numbers[1] = false; 31 | let primeNumbers = []; 32 | for (let i = 0; i < numbers.length; i++) { 33 | if (numbers[i]) { 34 | primeNumbers.push(i); 35 | for (let j = i; j <= n; j += i) { 36 | numbers[j] = false; 37 | } 38 | } 39 | } 40 | return primeNumbers; 41 | }; 42 | -------------------------------------------------------------------------------- /src/priority-queue/index.js: -------------------------------------------------------------------------------- 1 | const validateNumber = require('../validation/number'); 2 | 3 | class PriorityQueue { 4 | constructor() { 5 | this.queueArr = []; 6 | } 7 | 8 | // show the current queue 9 | queue() { 10 | return this.queueArr.join(' '); 11 | } 12 | 13 | // enqueue method 14 | push(elem) { 15 | validateNumber(elem, 'PriorityQueue.push'); 16 | 17 | let flag = false; 18 | 19 | for (let i in this.queueArr) { 20 | if (this.queueArr[i] > elem) { 21 | this.queueArr.splice(i, 0, elem); 22 | flag = true; 23 | break; 24 | } 25 | } 26 | 27 | if (!flag) { 28 | this.queueArr.push(elem); 29 | } 30 | 31 | return true; 32 | } 33 | 34 | // dequeue 35 | pop() { 36 | if (this.queueArr.length === 0) { 37 | return 'Queue Underflow'; 38 | } 39 | return this.queueArr.pop(); 40 | } 41 | 42 | // front of the priority queue 43 | front() { 44 | if (this.queueArr.length === 0) { 45 | return 'Queue Underflow'; 46 | } 47 | return this.queueArr[0]; 48 | } 49 | 50 | // rear of the priority queue 51 | rear() { 52 | if (this.queueArr.length === 0) { 53 | return 'Queue Underflow'; 54 | } 55 | return this.queueArr[this.queueArr.length - 1]; 56 | } 57 | 58 | // returns if the queue is empty or not 59 | empty() { 60 | return this.queueArr.length === 0 ? true : false; 61 | } 62 | 63 | // should return the queue size 64 | size() { 65 | return this.queueArr.length; 66 | } 67 | } 68 | 69 | module.exports = PriorityQueue; 70 | -------------------------------------------------------------------------------- /src/range/index.js: -------------------------------------------------------------------------------- 1 | /* Function: range() */ 2 | 3 | const validate = require('../validation/string'); 4 | 5 | module.exports = arg => { 6 | validate(arg,'range'); 7 | const number = require(`../${arg}`); 8 | if('range' in number){ 9 | return require(`../${arg}`).range; 10 | } 11 | else{ 12 | throw new TypeError(`Invalid argument received!`); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /src/smart/index.js: -------------------------------------------------------------------------------- 1 | /* Smart Number 2 | * Function: smart() 3 | */ 4 | 5 | const sort = require("../sort"); 6 | const validate = require('../validation/positive-integer'); 7 | 8 | exports.find = num => { 9 | validate(num, 'smart'); 10 | const size = 3000; 11 | var primes = new Array(size); 12 | for (var i = 0; i < size; i++) { 13 | primes[i] = 0; 14 | } 15 | var result = new Array(); 16 | for (var i = 2; i < size; i++) { 17 | if (primes[i] === 0) { 18 | primes[i] = 1; 19 | for (var j = i * 2; j < size; j = j + i) { 20 | primes[j] = primes[j] - 1; 21 | if (primes[j] + 3 === 0) { 22 | result.push(j); 23 | } 24 | } 25 | } 26 | } 27 | sort(result); 28 | return result[num - 1]; 29 | }; 30 | -------------------------------------------------------------------------------- /src/smith/index.js: -------------------------------------------------------------------------------- 1 | /* Smith Number 2 | * Function: isSmith() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | var primes = new Array(); 8 | const MAX = 10000; 9 | 10 | function sieveSundaram() { 11 | var marked = new Array(Math.floor(MAX / 2) + 100); 12 | for (let i = 0; i < Math.floor(MAX / 2) + 100; i++) { 13 | marked[i] = false; 14 | } 15 | 16 | for (let i = 1; i <= Math.floor((Math.sqrt(MAX) - 1) / 2); i++) { 17 | for ( 18 | let j = (i * (i + 1)) << 1; 19 | j <= Math.floor(MAX / 2); 20 | j = j + 2 * i + 1 21 | ) { 22 | marked[j] = true; 23 | } 24 | } 25 | primes.push(2); 26 | for (let i = 1; i <= Math.floor(MAX / 2); i++) { 27 | if (marked[i] === false) { 28 | primes.push(2 * i + 1); 29 | } 30 | } 31 | } 32 | 33 | function smith(num) { 34 | var originalNo = num; 35 | var pDigitSum = 0; 36 | for (let i = 0; primes[i] <= Math.floor(num / 2); i++) { 37 | while (num % primes[i] === 0) { 38 | var p = primes[i]; 39 | num = Math.floor(num / p); 40 | while (p > 0) { 41 | pDigitSum = pDigitSum + (p % 10); 42 | p = Math.floor(p / 10); 43 | } 44 | } 45 | } 46 | 47 | if (num !== 1 && num !== originalNo) { 48 | while (num > 0) { 49 | pDigitSum = pDigitSum + (num % 10); 50 | num = Math.floor(num / 10); 51 | } 52 | } 53 | 54 | var sumDigits = 0; 55 | while (originalNo > 0) { 56 | sumDigits = sumDigits + (originalNo % 10); 57 | originalNo = Math.floor(originalNo / 10); 58 | } 59 | return pDigitSum === sumDigits; 60 | } 61 | 62 | exports.check = num => { 63 | validate(num, 'isSmith'); 64 | sieveSundaram(); 65 | return smith(num); 66 | }; 67 | -------------------------------------------------------------------------------- /src/sort/index.js: -------------------------------------------------------------------------------- 1 | /* Function: sort() */ 2 | 3 | const validate = require('../validation/number-array'); 4 | 5 | function validateSecondArg(arg, order) { 6 | if(typeof order != 'boolean') 7 | throw new TypeError(`Invalid argument received: ${JSON.stringify(arg)}, ${order}\n'sort()' only accept an array of real numbers and boolean as the arguments!\n`); 8 | } 9 | 10 | module.exports = (arr, ascend = true) => { 11 | validate(arr, 'sort'); 12 | validateSecondArg(arr, ascend); 13 | return arr.sort((prev, next) => ascend ? prev - next : next - prev); 14 | }; 15 | -------------------------------------------------------------------------------- /src/stack/index.js: -------------------------------------------------------------------------------- 1 | const validate = require("../validation/number"); 2 | 3 | class Stack{ 4 | constructor(){ 5 | this.stack = []; 6 | } 7 | 8 | push(data){ 9 | validate(data, 'Stack.push'); 10 | this.stack.push(data); 11 | } 12 | 13 | pop(){ 14 | if(this.size() === 0){ 15 | return "Stack Underflow"; 16 | } 17 | 18 | else{ 19 | return this.stack.pop(); 20 | } 21 | } 22 | 23 | isEmpty(){ 24 | return (this.size() === 0); 25 | } 26 | 27 | head(){ 28 | if(this.size() === 0){ 29 | return "Empty Stack"; 30 | } 31 | else{ 32 | return this.stack[this.size() -1]; 33 | } 34 | } 35 | 36 | size(){ 37 | return this.stack.length; 38 | } 39 | 40 | copy(){ 41 | let internal = new Stack(); 42 | 43 | for(let i = 0; i { 31 | 32 | validate(n, 'isStormer'); 33 | let largPFactor = maxPrimeFactor((n*n+1)); 34 | 35 | return (largPFactor >= 2*n)?true:false; 36 | }; 37 | -------------------------------------------------------------------------------- /src/subtract/index.js: -------------------------------------------------------------------------------- 1 | /*Subtraction across all data-types and data-structures 2 | *Function: M.sub(m,n) 3 | */ 4 | const Complex = require('../complex'); 5 | const validate = require('../validation/argument-length'); 6 | 7 | const subNum = (a,b) => { 8 | return a - b; 9 | }; 10 | 11 | const subString = (a,b) => { 12 | let diff = Number(a) - Number(b); 13 | return diff.toString(10); 14 | }; 15 | 16 | const subArray = (a,b) => { 17 | let diff = new Array(a.length); 18 | for(let i = 0; i < a.length; i++){ 19 | let l = a[i] - b[i]; 20 | diff.push(l); 21 | diff = diff.filter(n => n); 22 | } 23 | return diff; 24 | }; 25 | 26 | const subMatrix = (a,b) => { 27 | let diff = [...Array(b.length)].map(x => Array(b[0].length).fill(0)); 28 | for(let i = 0; i < b.length; i++){ 29 | for(let j = 0; j < b[0].length; j++){ 30 | 31 | diff[i][j] = a[i][j] - b[i][j]; 32 | } 33 | } 34 | return diff; 35 | }; 36 | 37 | const subComplex = (a,b) => { 38 | let diff = { 39 | re: 0, 40 | im: 0 41 | }; 42 | diff.re = a.re - b.re; 43 | diff.im = a.im - b.im; 44 | return diff; 45 | }; 46 | 47 | const sub = (a,b) => { 48 | switch(a.constructor){ 49 | case Number: 50 | return subNum(a,b); 51 | case String: 52 | return subString(a,b); 53 | case Array: 54 | if(a[0].constructor === Array){ 55 | return subMatrix(a,b); 56 | } 57 | else { 58 | return subArray(a,b); 59 | } 60 | case Complex: 61 | return subComplex(a,b); 62 | } 63 | }; 64 | 65 | module.exports = (...args) => { 66 | if(args.length > 2){ 67 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n'sub()' only accepts two parameters!\n`); 68 | } 69 | validate(args); 70 | return sub(args[0], args[1]); 71 | }; 72 | -------------------------------------------------------------------------------- /src/sum/index.js: -------------------------------------------------------------------------------- 1 | /* Function: sum() */ 2 | 3 | const validate = require('../validation/number-array'); 4 | 5 | module.exports = arr => { 6 | validate(arr, 'sum'); 7 | return arr.reduce((prev, next) => prev + next, 0); 8 | }; 9 | -------------------------------------------------------------------------------- /src/totient/index.js: -------------------------------------------------------------------------------- 1 | /* count of numbers having gcd 1 till num 2 | * Function: phi() 3 | */ 4 | 5 | const validate = require('../validation/non-negative-integer'); 6 | 7 | function phi(num) { 8 | let result = num; 9 | 10 | for (let i = 3; i <= Math.sqrt(num); i = i + 1) { 11 | if (num % i == 0) { 12 | while (num % i == 0) { 13 | num = num / i; 14 | } 15 | result = result - result/i; 16 | } 17 | } 18 | 19 | if (num > 1) { 20 | result = result - result / num; 21 | } 22 | 23 | return result; 24 | } 25 | 26 | module.exports = num => { 27 | validate(num, 'phi'); 28 | return phi(num); 29 | }; 30 | -------------------------------------------------------------------------------- /src/triangular/index.js: -------------------------------------------------------------------------------- 1 | /*Triangular Number 2 | *Function: M.triangular() 3 | */ 4 | 5 | const validate = require('../validation/positive-integer'); 6 | 7 | const triangular = (n) => { 8 | let sum = 0; 9 | for(let i = 0; i < n; i++){ 10 | sum += (n - i); 11 | } 12 | return sum; 13 | }; 14 | 15 | exports.find = (n) => { 16 | validate(n, 'triangular'); 17 | return triangular(n); 18 | }; 19 | -------------------------------------------------------------------------------- /src/ugly/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Find nth Ugly Number 3 | */ 4 | const validate = require('../validation/positive-integer'); 5 | 6 | const maxDivide = (num, a) => { 7 | while (num % a === 0) { 8 | num /= a; 9 | } 10 | 11 | return num; 12 | }; 13 | 14 | const isUgly = (num) => { 15 | num = maxDivide(num, 2); 16 | num = maxDivide(num, 3); 17 | num = maxDivide(num, 5); 18 | 19 | return num === 1; 20 | }; 21 | 22 | const getNthUglyNumber = (n) => { 23 | let count = 1; 24 | let i = 1; 25 | 26 | while (n > count) { 27 | i += 1; 28 | 29 | if (isUgly(i)) { 30 | count += 1; 31 | } 32 | } 33 | 34 | return i; 35 | }; 36 | 37 | exports.find = (n) => { 38 | validate(n, 'ugly'); 39 | 40 | return getNthUglyNumber(n); 41 | }; 42 | -------------------------------------------------------------------------------- /src/union/index.js: -------------------------------------------------------------------------------- 1 | const validate = require('../validation/number-array'); 2 | 3 | let union = (arr1, arr2) => { 4 | 5 | validate(arr1, 'union'); 6 | validate(arr2, 'union'); 7 | 8 | arr1.sort(); 9 | arr2.sort(); 10 | 11 | let result = [], len = arr1.length + arr2.length, i = 0, j = 0; 12 | 13 | while((i+j) < len){ 14 | if((i < arr1.length && j < arr2.length)){ 15 | if(arr1[i] < arr2[j]){ 16 | result.push(arr1[i]); 17 | i++; 18 | } 19 | else if(arr1[i] > arr2[j]){ 20 | result.push(arr2[j]); 21 | j++; 22 | } 23 | else{ 24 | result.push(arr1[i]); 25 | i++; 26 | j++; 27 | } 28 | } 29 | else if(i == arr1.length){ 30 | result.push(arr2[j]); 31 | j++; 32 | } 33 | else{ 34 | result.push(arr1[i]); 35 | i++; 36 | } 37 | } 38 | return result; 39 | }; 40 | 41 | module.exports = union; -------------------------------------------------------------------------------- /src/validation/argument-length.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Argument and Length validation 3 | */ 4 | const Complex = require('../complex'); 5 | module.exports = (...args) => { 6 | if(args[0][0].constructor === Number){ 7 | for(let i = 0; i < args[0].length; i++){ 8 | if(typeof args[0][i] !== 'number'){ 9 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n, 'function' only accepts parameters of similar types!\n`) 10 | } 11 | } 12 | } 13 | else if(args[0][0].constructor === String){ 14 | for(let i = 0; i < args[0].length; i++){ 15 | if(typeof args[0][i] !== 'string'){ 16 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n 'function' only accepts parameters of similar types!\n`); 17 | } 18 | } 19 | } 20 | else if(args[0][0].constructor === Array){ 21 | let arr = args[0]; 22 | if(arr[0][0].constructor === Array){ 23 | let len = arr[0][0].length; 24 | for(let i = 0; i < arr.length; i++){ 25 | for(let j = 0; j < arr[0].length; j++){ 26 | if(arr[i][j].length !== len){ 27 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n 'function' only accepts parameters of similar length!\n`); 28 | } 29 | } 30 | } 31 | for(let i = 0; i < arr.length; i++){ 32 | for(let j = 0 ;j< arr[0].length; j++){ 33 | for(let k = 0; k < arr[0][0].length; k++){ 34 | if(typeof arr[i][j][k] !== 'number'){ 35 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n 'function' only accepts parameters of similar types!\n`); 36 | } 37 | } 38 | } 39 | } 40 | } 41 | else{ 42 | for(let i = 0; i < arr.length; i++){ 43 | for(let j = 0; j < arr.length; j++){ 44 | if(arr[i].length !== arr[j].length){ 45 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n 'function' only accepts parameters of similar length!\n`); 46 | } 47 | } 48 | } 49 | for(let i = 0; i < arr.length; i++){ 50 | for(let j = 0; j < arr[0].length; j++){ 51 | if(typeof arr[i][j] !== 'number'){ 52 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n 'function' only accepts parameters of similar types!\n`); 53 | } 54 | } 55 | } 56 | } 57 | 58 | } 59 | else if(args[0][0].constructor === Complex){ 60 | for(let i = 0; i < args[0].length; i++){ 61 | if(args[0][i].constructor !== Complex){ 62 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n 'function' only accepts parameters of similar types!\n`); 63 | } 64 | } 65 | } 66 | else{ 67 | throw new TypeError(`Invalid argument received: ${JSON.stringify(args)}\n`); 68 | } 69 | };//End of module exports 70 | -------------------------------------------------------------------------------- /src/validation/count.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Array and object validation 3 | */ 4 | 5 | module.exports = (arg, func) => { 6 | let isValid = false; 7 | 8 | if (typeof arg === 'string') { 9 | isValid = true; 10 | } 11 | else if (arg.constructor === Array) { 12 | isValid = true; 13 | } 14 | else if(arg.constructor === Object){ 15 | isValid = true; 16 | } 17 | else { 18 | isValid = false; 19 | } 20 | 21 | if (!isValid) { 22 | throw new TypeError( 23 | `Invalid argument received - ${JSON.stringify( 24 | arg 25 | )}\n'${func}()' only accept an Array or a String!\n` 26 | ); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /src/validation/frequency.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Array Validation 3 | */ 4 | 5 | module.exports = (arg, func) => { 6 | let isValid = false; 7 | 8 | if (typeof arg === 'string') { 9 | isValid = true; 10 | } else if (arg.constructor === Array) { 11 | isValid = true; 12 | } else { 13 | isValid = false; 14 | } 15 | 16 | if (!isValid) { 17 | throw new TypeError( 18 | `Invalid argument received - ${JSON.stringify( 19 | arg 20 | )}\n'${func}()' only accept an Array or a String!\n` 21 | ); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /src/validation/integer-array.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Integer Array Validation 3 | */ 4 | 5 | module.exports = (arg, func) => { 6 | let flag = 0; 7 | if(arg && arg.constructor == Array) 8 | arg.forEach(item => { 9 | if(typeof item != 'number' || item + 1 == item || item != item || !Number.isInteger(item) || item < 1) flag = 1; 10 | }); 11 | if(!arg || arg.constructor != Array || flag) 12 | throw new TypeError(`Invalid argument received: ${JSON.stringify(arg)}\n'${func}()' only accept an array of positive integers!\n`); 13 | } 14 | -------------------------------------------------------------------------------- /src/validation/integer-matrix.js: -------------------------------------------------------------------------------- 1 | /* 2 | Validation: Integer Matrix 3 | */ 4 | 5 | module.exports = (args, func) => { 6 | if(args.constructor !== Array || args[0].constructor !== Array){ 7 | throw new TypeError(`Invalid argument received: '${args}' , '${func}' only accepts a N-d array of numbers!`); 8 | } 9 | for(let i = 0; i < args.length; i++){ 10 | args[i].forEach((e) => { 11 | if(typeof e !== 'number'){ 12 | throw new TypeError(`Invalid argument received: '${args}' , '${func}' only accepts a N-d array of positive integers!`); 13 | } 14 | }); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /src/validation/integer-object.js: -------------------------------------------------------------------------------- 1 | //validate integer or object 2 | 3 | module.exports = (arg, func) => { 4 | if(typeof arg === 'boolean' || typeof arg === 'string' || arg.constructor === Array){ 5 | throw new TypeError(`Invalid argument received: ${JSON.stringify(arg)}\n'${func}()' only accepts an integer or an object!\n`); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/validation/integer.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Integer Validation 3 | */ 4 | 5 | module.exports = (arg, func) => { 6 | if(typeof arg != 'number' || arg + 1 == arg || arg != arg || !Number.isInteger(arg)) 7 | throw new TypeError(`Invalid argument received: ${JSON.stringify(arg)}\n'${func}()' only accept a positive integer!\n`); 8 | } 9 | -------------------------------------------------------------------------------- /src/validation/non-negative-integer.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Non-negative Integer Validation 3 | */ 4 | 5 | module.exports = (arg, func) => { 6 | if (typeof arg != "number" || arg < 0 || !Number.isInteger(arg)) { 7 | throw new TypeError( 8 | `Invalid argument received: ${JSON.stringify(arg)}\n'${func}()' only accepts a non-negative integer!\n` 9 | ); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/validation/number-array.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Number Array Validation 3 | */ 4 | 5 | module.exports = (arg, func) => { 6 | let flag = 0; 7 | if(arg && arg.constructor == Array) 8 | for(let item of arg) { 9 | if(typeof item != 'number' || item + 1 == item || item != item) { 10 | flag = 1; 11 | break; 12 | } 13 | } 14 | if(!arg || arg.constructor != Array || flag) 15 | throw new TypeError(`Invalid argument received: ${JSON.stringify(arg)}\n'${func}()' only accept an array of real numbers!\n`); 16 | }; 17 | -------------------------------------------------------------------------------- /src/validation/number.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Number Validation 3 | */ 4 | 5 | module.exports = (arg, func) => { 6 | if (typeof arg != 'number') { 7 | throw new TypeError( 8 | `Invalid argument received - ${JSON.stringify( 9 | arg 10 | )}\n'${func}()' only accept a number!\n` 11 | ); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /src/validation/pipe.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Pipe Validation 3 | */ 4 | 5 | module.exports = (arg, func) => { 6 | if(arg === undefined) 7 | throw new TypeError(`Invalid argument received: ${JSON.stringify(arg)}\n'${func}()' only accept an array of functions!\n`); 8 | } 9 | -------------------------------------------------------------------------------- /src/validation/positive-integer.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Positive Integer Validation 3 | */ 4 | 5 | module.exports = (arg, func) => { 6 | if(typeof arg != 'number' || arg + 1 == arg || arg != arg || arg < 1 || !Number.isInteger(arg)) 7 | throw new TypeError(`Invalid argument received - ${JSON.stringify(arg)}\n'${func}()' only accept a positive integer!\n`); 8 | } 9 | -------------------------------------------------------------------------------- /src/validation/string.js: -------------------------------------------------------------------------------- 1 | /* 2 | * String Validation 3 | */ 4 | 5 | module.exports = (arg, func) => { 6 | if(typeof arg != 'string'){ 7 | throw new TypeError(`Invalid argument received: ${JSON.stringify(arg)}\n'${func}()' only accept a string!\n`); 8 | } 9 | }; -------------------------------------------------------------------------------- /test/absolute-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | abs = require('../src/absolute'), 3 | Complex = require('../src/complex'), 4 | a = new Complex(7,3), 5 | b = new Complex(3,4); 6 | 7 | describe('[Function: abs]', () => { 8 | it('should return a function', () => { 9 | assert.strictEqual(typeof abs, 'function'); 10 | }); 11 | 12 | it('should throw a TypeError if no parameter is passed', () => { 13 | assert.throws(() => abs(), TypeError); 14 | }); 15 | 16 | it('should throw a TypeError when two parameters are passed', () => { 17 | assert.throws(() => abs(1,2), TypeError); 18 | }); 19 | 20 | it('should return 14 when -14 is passed', () => { 21 | assert.strictEqual(abs(-14), 14); 22 | }); 23 | 24 | it('should return 12.24 when -12.24 is passed', () => { 25 | assert.strictEqual(abs(-12.24), 12.24); 26 | }); 27 | 28 | it('should throw an error when (1,2,4) is passed', () => { 29 | assert.throws(() => abs(1,2,4), TypeError); 30 | }); 31 | 32 | it('should return "20" when string "-20" is passed', () => { 33 | assert.strictEqual(abs('-20'), "20"); 34 | }); 35 | 36 | it('should return 0 when [12,9] is passed', () => { 37 | assert.deepStrictEqual(abs([12,9]), 0); 38 | }); 39 | 40 | it('should return 2 when [[2]] is passed', () => { 41 | assert.strictEqual(abs([[2]]), 2); 42 | }); 43 | 44 | it('should return 12 when ([[3,2],[5,4]]) is passed', () => { 45 | assert.deepStrictEqual(abs([[3,2],[5,4]]), 2); 46 | }); 47 | 48 | it('should return 306 when [[6,1,1],[4,-2,5],[2,8,7]] is passed', () => { 49 | assert.strictEqual(abs([[6,1,1],[4,-2,5],[2,8,7]]), 306); 50 | }); 51 | 52 | it('should return 7.62 when Complex objects a are passed', () => { 53 | assert.deepStrictEqual(abs(a), 7.62); 54 | }); 55 | 56 | it('should return 5 when Complex object b is passed', () => { 57 | assert.strictEqual(abs(b), 5); 58 | }); 59 | 60 | it('should throw an error when boolean values are passed', () => { 61 | assert.throws(() => abs(false), TypeError); 62 | }); 63 | 64 | it('should throw an error when wrong dimensions of matrix is passed [[1,2,3,4],[1,2,3,4]]', () => { 65 | assert.throws(() => abs([[1,2,3,4],[1,2,3,4]]), TypeError); 66 | }); 67 | 68 | it('should return 5 when 5 is passed', () => { 69 | assert.strictEqual(abs(5), 5); 70 | }); 71 | 72 | it('should return "9" when "9" is passed', () => { 73 | assert.strictEqual(abs('9'), "9"); 74 | }); 75 | }); 76 | -------------------------------------------------------------------------------- /test/add-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | add = require('../src/add'), 3 | Complex = require('../src/complex'), 4 | a = new Complex(7,3), 5 | b = new Complex(4,6); 6 | 7 | describe('[Function: add]', () => { 8 | it('should return a function', () => { 9 | assert.strictEqual(typeof add, 'function'); 10 | }); 11 | 12 | it('should throw a TypeError if no parameter is passed', () => { 13 | assert.throws(() => add(), TypeError); 14 | }); 15 | 16 | it('should throw a TypeError when one parameter is passed', () => { 17 | assert.throws(() => add([1,2,3,4]), TypeError); 18 | }); 19 | 20 | it('should return -6 when (-2,-4 is passed', () => { 21 | assert.strictEqual(add(-2,-4), -6); 22 | }); 23 | 24 | it('should return 15 when (1,2,3,4,5) is passed', () => { 25 | assert.strictEqual(add(1,2,3,4,5), 15); 26 | }); 27 | 28 | it('should return 12.2 when (1.2,4.5,6.5) is passed', () => { 29 | assert.strictEqual(add(1.2,4.5,6.5), 12.2); 30 | }); 31 | 32 | it('should return 15 when strings (1,2,3,4,5) is passed', () => { 33 | assert.strictEqual(add('1','2','3','4','5'), "15"); 34 | }); 35 | 36 | it('should return [2,4,6,8,10] when [1,2,3,4,5],[1,2,3,4,5] is passed', () => { 37 | assert.deepStrictEqual(add([1,2,3,4,5],[1,2,3,4,5]), [2,4,6,8,10]); 38 | }); 39 | 40 | it('should return [3,6,9,12,15] when [1,2,3,4,5] is passed thrice', () => { 41 | assert.deepStrictEqual(add([1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]), [3,6,9,12,15]); 42 | }); 43 | 44 | it('should return [[14]] when ([[1]],[[5]],[[8]]) is passed', () => { 45 | assert.deepStrictEqual(add([[1]], [[5]], [[8]]), [[14]]); 46 | }); 47 | 48 | it('should return [[1,5],[10,6]] when [[1,2],[2,5]],[[0,3],[8,1]] is passed', () => { 49 | assert.deepStrictEqual(add([[1,2],[2,5]],[[0,3],[8,1]]), [[1,5], [10,6]]); 50 | }); 51 | 52 | it('should return an object when Complex objects a,b are passed', () => { 53 | assert.deepStrictEqual(add(a,b), {re: 11, im: 9}); 54 | }); 55 | 56 | it('should throw an error when boolean values are passed', () => { 57 | assert.throws(() => add(false, true, false), TypeError); 58 | }); 59 | 60 | it('should throw an error when there is type difference', () => { 61 | assert.throws(() => add(1,2,4,'6',7), TypeError); 62 | }); 63 | 64 | it('should throw an error when there is type difference', () => { 65 | assert.throws(() => add(1.2,1.4,[1,2,3],5.6), TypeError); 66 | }); 67 | 68 | it('should throw an error when there is a type difference', () => { 69 | assert.throws(() => add('1','2', 3, '4'), TypeError); 70 | }); 71 | 72 | it('should throws an error when the object is not of Complex', () => { 73 | assert.throws(() => add(a,{p: 30, r: 20}), TypeError); 74 | }); 75 | 76 | it('should throw an error when no object match', () => { 77 | assert.throws(() => add({p: 30, q: 20}, a, {r: 10, s: 5}), TypeError); 78 | }); 79 | 80 | it('should throw an error when matrix has a length difference', () => { 81 | assert.throws(() => add([[1]], [[1,2,3]], [[8]]), TypeError); 82 | }); 83 | 84 | it('should throw an error when matrix has a type difference', () => { 85 | assert.throws(() => add([[2]], [['5']], [['8']]), TypeError); 86 | }); 87 | 88 | it('should throw an error when 1D array has length difference', () => { 89 | assert.throws(() => add([1,2,3,4,5],[1,2,3,4]), TypeError); 90 | }); 91 | 92 | it('should throw an error when 1D array has type difference', () => { 93 | assert.throws(() => add([1,2,3,'4',5],[1,2,3,4,5]), TypeError); 94 | }); 95 | 96 | 97 | }); 98 | -------------------------------------------------------------------------------- /test/armstrong-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | isArmstrong = require('../src/armstrong').check; 3 | 4 | describe('[Function: isArmstrong]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof isArmstrong, 'function'); 8 | }); 9 | 10 | it('should return a boolean value when a positive integer is passed', () => { 11 | assert.strictEqual(typeof isArmstrong(1), 'boolean'); 12 | }); 13 | 14 | it('should return \'true\' when \'153\' is passed', () => { 15 | assert.strictEqual(isArmstrong(153), true); 16 | }); 17 | 18 | it('should return \'false\' when \'34\' is passed', () => { 19 | assert.strictEqual(isArmstrong(34), false); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => isArmstrong(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => isArmstrong(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => isArmstrong('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => isArmstrong(), TypeError); 36 | }); 37 | 38 | }); 39 | -------------------------------------------------------------------------------- /test/automorphic-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | isAutomorphic = require('../src/automorphic').check; 3 | 4 | describe('[Function: isAutomorphic]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof isAutomorphic, 'function'); 8 | }); 9 | 10 | it('should return a boolean value when a positive integer is passed', () => { 11 | assert.strictEqual(typeof isAutomorphic(1), 'boolean'); 12 | }); 13 | 14 | it('should return \'true\' when \'25\' is passed', () => { 15 | assert.strictEqual(isAutomorphic(25), true); 16 | }); 17 | 18 | it('should return \'false\' when \'14\' is passed', () => { 19 | assert.strictEqual(isAutomorphic(14), false); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => isAutomorphic(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => isAutomorphic(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => isAutomorphic('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => isAutomorphic(), TypeError); 36 | }); 37 | 38 | }); 39 | -------------------------------------------------------------------------------- /test/average-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | avg = require('../src/average'); 3 | 4 | describe('[Function: avg]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof avg, 'function'); 8 | }); 9 | 10 | it('should return a number when an array of number is passed', () => { 11 | assert.strictEqual(typeof avg([1, 1]), 'number'); 12 | }); 13 | 14 | it('should return \'13.93333\' when \'[12.57, 5.23, 24]\' is passed', () => { 15 | assert.strictEqual(avg([12.57, 5.23, 24]), 13.93333); 16 | }); 17 | 18 | it('should return \'0\' when \'[-2, -1, 1, 2]\' is passed', () => { 19 | assert.strictEqual(avg([-2, -1, 1, 2]), 0); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => avg(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => avg(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => avg('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => avg(), TypeError); 36 | }); 37 | 38 | }); 39 | -------------------------------------------------------------------------------- /test/bell-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"), 2 | bell = require("../src/bell").find; 3 | 4 | describe("[Function: bell]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof bell, "function"); 7 | }); 8 | 9 | it("should throw an error when a negative number is passed", () => { 10 | assert.throws(() => bell(-37), TypeError); 11 | }); 12 | 13 | it("should throw an error when a floating point is passed", () => { 14 | assert.throws(() => bell(3.141718), TypeError); 15 | }); 16 | 17 | it("should throw an error when a string is passed", () => { 18 | assert.throws(() => bell("26"), TypeError); 19 | }); 20 | 21 | it("should throw an error when a boolean is passed", () => { 22 | assert.throws(() => bell(true), TypeError); 23 | }); 24 | 25 | it("should throw an error when an array is passed", () => { 26 | assert.throws(() => bell([3, 4]), TypeError); 27 | }); 28 | 29 | it("should throw an error when no arguments passed", () => { 30 | assert.throws(() => bell(), TypeError); 31 | }); 32 | 33 | it("should throw an error when an object arguments passed", () => { 34 | assert.throws(() => bell({1 : 2}), TypeError); 35 | }); 36 | 37 | it("should return a number when a non-negative integer is passed", () => { 38 | assert.strictEqual(typeof bell(3), "number"); 39 | }); 40 | 41 | it("should return '1' when '0' is passed", () => { 42 | assert.strictEqual(bell(0), 1); 43 | }); 44 | 45 | it("should return '1' when '1' is passed", () => { 46 | assert.strictEqual(bell(1), 1); 47 | }); 48 | 49 | it("should return '52' when '5' is passed", () => { 50 | assert.strictEqual(bell(5), 52); 51 | }); 52 | }); 53 | -------------------------------------------------------------------------------- /test/binomial-coeff-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | nCr = require('../src/binomial-coeff'); 3 | 4 | describe('[Function: nCr]', () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof nCr, 'function'); 7 | }); 8 | 9 | it('should return 10 when 5 and 2 is passed', () => { 10 | assert.strictEqual(nCr(5, 2), 10); 11 | }); 12 | 13 | it('should return 3 when 3 and 2 is passed', () => { 14 | assert.strictEqual(nCr(3, 2), 3); 15 | }); 16 | 17 | it('should throw an error when a negative number is passed', () => { 18 | assert.throws(() => nCr(-2, 1), TypeError); 19 | }); 20 | 21 | it('should throw an error when first number is smaller than second is passed', () => { 22 | assert.throws(() => nCr(2, 3), TypeError); 23 | }); 24 | 25 | it('should throw an error when a floating point is passed', () => { 26 | assert.throws(() => nCr(31.101996, 2), TypeError); 27 | }); 28 | 29 | it('should throw an error when a string is passed', () => { 30 | assert.throws(() => nCr('31', 2), TypeError); 31 | }); 32 | 33 | it('should throw an error when no arguments passed', () => { 34 | assert.throws(() => nCr(), TypeError); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /test/carmichael-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"), 2 | isCarmichael = require("../src/carmichael").check; 3 | 4 | describe("[Function: isCarmichael]", () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof isCarmichael, 'function'); 7 | }); 8 | 9 | it('should return a boolean value when a positive integer is passed', () => { 10 | assert.strictEqual(typeof isCarmichael(8), 'boolean'); 11 | }); 12 | 13 | it('should return true when 561 is passed', () => { 14 | assert.strictEqual(isCarmichael(561), true); 15 | }); 16 | 17 | it('should return false if 8 is passed', () => { 18 | assert.strictEqual(isCarmichael(8), false); 19 | }); 20 | 21 | it('should throw an error when a negative number is passed', () => { 22 | assert.throws(() => isCarmichael(-10), TypeError); 23 | }); 24 | 25 | it('should throw an error when a floating point is passed', () => { 26 | assert.throws(() => isCarmichael(7.2), TypeError); 27 | }); 28 | 29 | it('should throw an error when a string is passed', () => { 30 | assert.throws(() => isCarmichael('hello'), TypeError); 31 | }); 32 | 33 | it('should throw an error when no arguments are passed', () => { 34 | assert.throws(() => isCarmichael(), TypeError); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /test/catalan-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"), 2 | catalan = require("../src/catalan").find; 3 | 4 | describe("[Function: catalan]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof catalan, "function"); 7 | }); 8 | 9 | it("should return a number when a non-negative integer is passed", () => { 10 | assert.strictEqual(typeof catalan(1), "number"); 11 | }); 12 | 13 | it("should return '1' when '0' is passed", () => { 14 | assert.strictEqual(catalan(0), 1); 15 | }); 16 | 17 | it("should return '4862' when '9' is passed", () => { 18 | assert.strictEqual(catalan(9), 4862); 19 | }); 20 | 21 | it("should return '42' when '5' is passed", () => { 22 | assert.strictEqual(catalan(5), 42); 23 | }); 24 | 25 | it("should return '429' when '7' is passed", () => { 26 | assert.strictEqual(catalan(7), 429); 27 | }); 28 | 29 | it("should throw an error when a negative number is passed", () => { 30 | assert.throws(() => catalan(-20), TypeError); 31 | }); 32 | 33 | it("should throw an error when a floating point is passed", () => { 34 | assert.throws(() => catalan(31.101996), TypeError); 35 | }); 36 | 37 | it("should throw an error when a string is passed", () => { 38 | assert.throws(() => catalan("31"), TypeError); 39 | }); 40 | 41 | it("should throw an error when a boolean is passed", () => { 42 | assert.throws(() => catalan(true), TypeError); 43 | }); 44 | 45 | it("should throw an error when an array is passed", () => { 46 | assert.throws(() => catalan([1, 2]), TypeError); 47 | }); 48 | 49 | it("should throw an error when no arguments passed", () => { 50 | assert.throws(() => catalan(), TypeError); 51 | }); 52 | }); 53 | -------------------------------------------------------------------------------- /test/check-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"), 2 | check = require("../src/check"); 3 | 4 | describe("[Function: check]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof check, "function"); 7 | }); 8 | 9 | it('should return `true` when `armstrong` is called with 153', () => { 10 | assert.strictEqual(check('armstrong')(153), true); 11 | }); 12 | 13 | it('should return true when automorphic is called with 76', () => { 14 | assert.strictEqual(check('automorphic')(76), true); 15 | }); 16 | 17 | it('should return true when harshad is called with 3', () => { 18 | assert.strictEqual(check('harshad')(3), true); 19 | }); 20 | 21 | it('should return true when stormer is called with 4', () => { 22 | assert.strictEqual(check('stormer')(4), true); 23 | }); 24 | 25 | it('should return true when carmichael is called with 561', () => { 26 | assert.strictEqual(check('carmichael')(561), true); 27 | }); 28 | 29 | it('should return true when deficient is called with 21', () => { 30 | assert.strictEqual(check('deficient')(21), true); 31 | }); 32 | 33 | it('should return true when frugal is called with 125', () => { 34 | assert.strictEqual(check('frugal')(125), true); 35 | }); 36 | 37 | it('should retrun true when kaprekar is called with 45', () => { 38 | assert.strictEqual(check('kaprekar')(45), true); 39 | }); 40 | 41 | it('should return true when lucky is called with 3', () => { 42 | assert.strictEqual(check('lucky')(3), true); 43 | }); 44 | 45 | it('should return true when neon is called with 9', () => { 46 | assert.strictEqual(check('neon')(9), true); 47 | }); 48 | 49 | it('should return true when perfect is called with 6', () => { 50 | assert.strictEqual(check('perfect')(6), true); 51 | }); 52 | 53 | it('should return true when smith is called with 4', () => { 54 | assert.strictEqual(check('smith')(4), true); 55 | }); 56 | 57 | it('should return true when hoax is called with 22', () => { 58 | assert.strictEqual(check('hoax')(22), true); 59 | }); 60 | 61 | it('should return true when palindrome is called with 121', () => { 62 | assert.strictEqual(check('palindrome')(121), true); 63 | }); 64 | 65 | it("should return `true` when called `prime` with 2 as an argument", () => { 66 | assert.strictEqual(check('prime')(2), true); 67 | }); 68 | 69 | it('should throw an error when sum is called', () => { 70 | assert.throws(() => check('sum')([2,3,4]), TypeError); 71 | }); 72 | }); 73 | -------------------------------------------------------------------------------- /test/complex-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | Complex = require('../src/complex'), 3 | a = new Complex(3,2), 4 | b = new Complex(2,1), 5 | conjugate = a.conj, 6 | add1 = a.add(5), 7 | add2 = a.add(b), 8 | sub1 = a.sub(1), 9 | sub2 = a.sub(b), 10 | mul = a.multiply(b), 11 | mul1 = a.multiply(4), 12 | div = a.divide(b), 13 | div1 = a.divide(3); 14 | 15 | describe('[Function: complex]', () => { 16 | it('should be an object', () => { 17 | assert.strictEqual(typeof a, 'object'); 18 | 19 | }); 20 | 21 | it('should return 3 when a.re is called', () => { 22 | assert.strictEqual(a.re, 3); 23 | }); 24 | 25 | it('should return 2 when a.im is called', () => { 26 | assert.strictEqual(a.im, 2); 27 | }); 28 | 29 | it('should return something when a.abs() is called', () => { 30 | assert.strictEqual(a.abs(), 3.61); 31 | }); 32 | 33 | it('should return an object when a.add(5) is called', () => { 34 | assert.strictEqual(typeof add1, 'object'); 35 | }); 36 | 37 | it('should return 8 when add1.re is called', () => { 38 | assert.strictEqual(add1.re, 8); 39 | }); 40 | 41 | it('should return an object when a.add(b) is called', () => { 42 | assert.strictEqual(typeof add2, 'object'); 43 | }); 44 | 45 | it('should return 5 when add2.re is called', () => { 46 | assert.strictEqual(add2.re, 5); 47 | }); 48 | 49 | it('should return 3 when add2.im is called', () => { 50 | assert.strictEqual(add2.im, 3); 51 | }); 52 | 53 | it('should return an object when a.conj is called', () => { 54 | assert.strictEqual(typeof conjugate, 'object'); 55 | }); 56 | 57 | it('should return -2 if conjugate.im is called', () => { 58 | assert.strictEqual(conjugate.im, -2); 59 | }); 60 | 61 | it('should return 3 when conjugate.re is called', () => { 62 | assert.strictEqual(conjugate.re, 3); 63 | }); 64 | 65 | it('should return an object when a.sub(1) is called', () => { 66 | assert.strictEqual(typeof sub1, 'object'); 67 | }); 68 | 69 | it('should return 2 when sub1.re is called', () => { 70 | assert.strictEqual(sub1.re , 2); 71 | }); 72 | 73 | it('should return 2 when sub1.im is called', () => { 74 | assert.strictEqual(sub1.im, 2); 75 | }); 76 | 77 | it('should return an object when a.sub(b) is called', () => { 78 | assert.strictEqual(typeof sub2, 'object'); 79 | }); 80 | 81 | it('should return 1 when sub2.re is called', () => { 82 | assert.strictEqual(sub2.re, 1); 83 | }); 84 | 85 | it('should return 1 when sub2.im is called', () => { 86 | assert.strictEqual(sub2.im, 1); 87 | }); 88 | 89 | it('should return an object when a.multiply(b) is called', () => { 90 | assert.strictEqual(typeof mul, 'object'); 91 | }); 92 | 93 | it('should return 4 when mul.re is called', () => { 94 | assert.strictEqual(mul.re, 4); 95 | }); 96 | 97 | it('should return 7 when mul.im is called', () => { 98 | assert.strictEqual(mul.im, 7); 99 | }); 100 | 101 | it('should return 12 when mul1.re is called', () => { 102 | assert.strictEqual(mul1.re, 12); 103 | }); 104 | 105 | it('should return an object when a.divide(b) is called', () => { 106 | assert.strictEqual(typeof a.divide(b), 'object'); 107 | }); 108 | 109 | it('should return 1.6 when div.re is called', () => { 110 | assert.strictEqual(div.re, 1.6); 111 | }); 112 | 113 | it('should return 0.2 when div.im is called', () => { 114 | assert.strictEqual(div.im, 0.2); 115 | }); 116 | 117 | it('should return 1 when div1.re is called', () => { 118 | assert.strictEqual(div1.re, 1); 119 | }); 120 | 121 | it('should throw an error when argument passed is string', () => { 122 | assert.throws(() => { 123 | const an = new Complex("4", "3"); 124 | } 125 | , TypeError); 126 | }); 127 | 128 | it('should throw an error when a non-number is passed', () => { 129 | assert.throws(() => { 130 | const b = new Complex(2,4); 131 | b.add([3]); 132 | } 133 | , TypeError); 134 | }); 135 | }); 136 | -------------------------------------------------------------------------------- /test/convert-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | convert = require('../src/convert'); 3 | 4 | describe("Function: convert", () => { 5 | 6 | it("should be a function", () => { 7 | assert.strictEqual(typeof convert, "function"); 8 | }); 9 | 10 | it("should throw an error when a string is passed", () => { 11 | assert.throws(() => convert("31", {from: 'degree', to: 'radian'}), TypeError); 12 | }); 13 | 14 | it("should throw an error when a boolean is passed", () => { 15 | assert.throws(() => convert(true, {from: 'degree', to: 'radian'}), TypeError); 16 | }); 17 | 18 | it("should throw an error when a string is passed", () => { 19 | assert.throws(() => convert("31", {from: 'radian', to: 'degree'}), TypeError); 20 | }); 21 | 22 | it("should throw an error when a boolean is passed", () => { 23 | assert.throws(() => convert(true, {from: 'radian', to: 'degree'}), TypeError); 24 | }); 25 | 26 | it("should throw an error when an object is not passed", () => { 27 | assert.throws(() => convert(true, 6), TypeError); 28 | }); 29 | 30 | it("should throw an error when a to/from is(are) absent", () => { 31 | assert.throws(() => convert(true, {from: 'radian', an: 'degree'}), ReferenceError); 32 | }); 33 | 34 | it("should throw an error when a to/from is(are) absent", () => { 35 | assert.throws(() => convert(true, {fom: 'radian', an: 'degree'}), ReferenceError); 36 | }); 37 | 38 | it("should throw an error when a to/from is(are) absent", () => { 39 | assert.throws(() => convert(true, {a: 'radian', an: 'degree'}), ReferenceError); 40 | }); 41 | 42 | it("should throw an error when value of trim is not a number", () => { 43 | assert.throws(() => convert(true, {from: 'radian', to: 'degree'}, 'a'), TypeError); 44 | }); 45 | 46 | it("should return a message when any convertion type not available is passed", () => { 47 | assert.strictEqual(convert(5, {from: 'degree', to: 'degree'}), "Your to/from parameter was not found!"); 48 | }); 49 | 50 | it("should return a number", () => { 51 | assert.strictEqual(typeof convert(5, {from: 'degree', to: 'radian'}), "number"); 52 | }); 53 | 54 | it("should return a number", () => { 55 | assert.strictEqual(typeof convert(5, {from: 'radian', to: 'degree'}), "number"); 56 | }); 57 | 58 | it("should return '3.141592653589793' when '180' is passed", () => { 59 | assert.strictEqual(convert(180, {from: 'degree', to: 'radian'}), Math.PI); 60 | }); 61 | 62 | it("should return '3.141592653589793' when '180' is passed and trim = 0", () => { 63 | assert.strictEqual(convert(180, {from: 'degree', to: 'radian'}, 0), Math.PI); 64 | }); 65 | 66 | it("should return '3.14159265' when '180' is passed and trim = 8", () => { 67 | assert.strictEqual(convert(180, {from: 'degree', to: 'radian'}, 8), 3.14159265); 68 | }); 69 | 70 | it("should return '75.00017538262475' when '1.309' is passed", () => { 71 | assert.strictEqual(convert(1.309, {from: 'radian', to: 'degree'}), 75.00017538262475); 72 | }); 73 | 74 | it("should return '75.00017538262475' when '1.309' is passed with trim = 0", () => { 75 | assert.strictEqual(convert(1.309, {from: 'radian', to: 'degree'}, 0), 75.00017538262475); 76 | }); 77 | 78 | it("should return '75.000175' when '1.309' is passed and trim = 6", () => { 79 | assert.strictEqual(convert(1.309, {from: 'radian', to: 'degree'}, 6), 75.000175); 80 | }); 81 | 82 | }); -------------------------------------------------------------------------------- /test/count-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | count = require('../src/count/index'); 3 | 4 | describe('[Function: count]', () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof count, 'function'); 7 | }); 8 | 9 | it('should return 3 when the following array and occurance is passed', () => { 10 | assert.strictEqual(count([1, 2, 3, 2, 1, 2], 2), 3); 11 | }); 12 | 13 | it('should return 1 when the following string and occurance is passed', () => { 14 | assert.strictEqual(count('testing count', 'es'), 1); 15 | }); 16 | 17 | it('should return 2 when the following string and occurance is passed', () => { 18 | assert.strictEqual(count(['testing', 'count', 'testing'], 'testing'),2); 19 | }); 20 | 21 | it('should return 3 when the following array and occurance is passed', () => { 22 | assert.strictEqual(count([1, 2, 3, 1, 2, 4, 5, 1], 1), 3); 23 | }); 24 | 25 | it('should return 2 when the following array and occurance is passed', () => { 26 | assert.strictEqual(count(['a', 'y', 'a', 'b', 'c', 'z', 'i'], 'a'), 2); 27 | }); 28 | 29 | it('should return 1 when the following array and occurance is passed', () => { 30 | assert.strictEqual(count(['hello', 'world', 'lorem', 'ipsum', 'hello'], 'world'),1); 31 | }); 32 | 33 | it('should return 0 when the following array and occurance is passed', () => { 34 | assert.strictEqual(count(['hello', 5, 'lorem', 7, 'ipsum', 'hello'], 'world'),0); 35 | }); 36 | 37 | it('should return 2 when the following string and occurance is passed', () => { 38 | assert.strictEqual(count('banana', 'ana'), 2); 39 | }); 40 | 41 | //Object type testing part 42 | it('should return 1 when the following object and occurance is passed', () => { 43 | assert.strictEqual(count({'ipsum': [true, 1]}, [true, 1]), 1); 44 | }); 45 | 46 | it('should return 0 when the following object and occurance is passed', () => { 47 | assert.strictEqual(count({'ipsum': [true, 1]}, [false, 1]), 0); 48 | }); 49 | 50 | it('should return 1 when nested object and occurance is passed', () => { 51 | assert.strictEqual(count({'ipsum': {true: 1}}, {true: 1}), 1); 52 | }); 53 | 54 | it('should return 0 when nested object and occurance is passed', () => { 55 | assert.strictEqual(count({'ipsum': {true: 1}}, {true: 2}), 0); 56 | }); 57 | 58 | it('should return 0 when nested object and occurance is passed', () => { 59 | assert.strictEqual(count({'ipsum': {true: 1}}, {false: 1}), 0); 60 | }); 61 | 62 | it('should return 0 when the following object and occurance is passed', () => { 63 | assert.strictEqual(count({'ipsum': [true, 1]}, [true]), 0); 64 | }); 65 | 66 | it('should return 0 when the following object and occurance is passed flag', () => { 67 | assert.strictEqual(count({'ipsum': [true, 1]}, [true, 2]), 0); 68 | }); 69 | 70 | it('should return 2 when the following object and occurance is passed', () => { 71 | assert.strictEqual(count({'foo': 'bar', 'spam': 'egg', 'lorem': 'bar'}, 'bar'), 2); 72 | }); 73 | 74 | it('should throw TypeError when a number is passed as arguments', () => { 75 | assert.throws(() => count(87), TypeError); 76 | }); 77 | 78 | it('should throw TypeError when nothing is passed as arguments', () => { 79 | assert.throws(() => count(), TypeError); 80 | }); 81 | 82 | it('should throw a TypeError when more than 2 parameters are passed', () => { 83 | assert.throws(() => count(1,2,3), TypeError); 84 | }); 85 | }); 86 | -------------------------------------------------------------------------------- /test/deficient-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | deficient = require('../src/deficient').check; 3 | 4 | describe('[Function: deficient]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof deficient, 'function'); 8 | }); 9 | 10 | it('should throw TypeError when a negative number is passed as argument', () => { 11 | assert.throws(() => deficient(-10), TypeError); 12 | }); 13 | 14 | it('should throw TypeError when a string is passed as argument', () => { 15 | assert.throws(() => deficient('hello world'), TypeError); 16 | }); 17 | 18 | it('should throw TypeError when an array is passed as argument', () => { 19 | assert.throws(() => deficient(['foo', 'bar', 1, 3]), TypeError); 20 | }); 21 | 22 | it('should throw TypeError when an object is passed as argument', () => { 23 | assert.throws(() => deficient({ foo: 'bar' }), TypeError); 24 | }); 25 | 26 | it('should throw TypeError when a function is passed as argument', () => { 27 | assert.throws(() => deficient(deficient), TypeError); 28 | }); 29 | 30 | it('should return true when 10 is passed as an argument', () => { 31 | assert.strictEqual(deficient(10), true); 32 | }); 33 | 34 | it('should return false when 6 is passed as an argument', () => { 35 | assert.strictEqual(deficient(6), false); 36 | }); 37 | 38 | it('should return true when 1 is passed as an argument', () => { 39 | assert.strictEqual(deficient(1), true); // as always, 1 is a special case 40 | }); 41 | }); 42 | -------------------------------------------------------------------------------- /test/divide-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | div = require('../src/divide'), 3 | Complex = require('../src/complex'), 4 | a = new Complex(7,3), 5 | b = new Complex(4,6); 6 | 7 | describe('[Function: div]', () => { 8 | it('should return a function', () => { 9 | assert.strictEqual(typeof div, 'function'); 10 | }); 11 | it('should throw a TypeError if no parameter is passed', () => { 12 | assert.throws(() => div(), TypeError); 13 | }); 14 | it('should throw a TypeError when one parameter is passed', () => { 15 | assert.throws(() => div([1,2,3,4]), TypeError); 16 | }); 17 | it('should return 1 when (4,2) is passed', () => { 18 | assert.strictEqual(div(4,2), 2); 19 | }); 20 | it('should return 4.2 when (8.4, 2.0) is passed', () => { 21 | assert.strictEqual(div(8.4,2.0), 4.2); 22 | }); 23 | it('should throw an error when (1,2,4) is passed', () => { 24 | assert.throws(() => div(1,2,4), TypeError); 25 | }); 26 | it('should return "4.2" when strings (8.4,2.0) is passed', () => { 27 | assert.strictEqual(div('8.4','2.0'), "4.2"); 28 | }); 29 | it('should return [2,3] when [12,9],[6,3] is passed', () => { 30 | assert.deepStrictEqual(div([12,9],[6,3]), [2,3]); 31 | }); 32 | it('should return 0 if determinant is zero', () => { 33 | assert.deepStrictEqual(div([[0,0]],[[0,0]]), 0); 34 | }); 35 | it('should return 1 when 1x1 matrix is passed', () => { 36 | assert.strictEqual(div([[4]],[[4]]), 1) 37 | }); 38 | it('should return [[1,0],[0,1]] when ([[4,3],[3,2]],[[4,3], [3,2]]) is passed', () => { 39 | assert.deepStrictEqual(div([[4,3],[3,2]], [[4,3],[3,2]]), [[1,0], [0,1]]); 40 | }); 41 | it('should return [[1,-0,-0],[-0,1,-0],[-0,-0,1]] when same 3x3 matrix is passed', () => { 42 | assert.deepStrictEqual(div([[4,7,2],[2,6,4],[1,2,3]], [[4,7,2],[2,6,4],[1,2,3]]), [[1,-0,-0],[-0,1,-0],[-0,-0,1]]); 43 | }); 44 | it('should return an object when Complex objects a,b are passed', () => { 45 | assert.deepStrictEqual(div(a,b), {re: 0.77, im: -0.58}); 46 | }); 47 | it('should throw an error when boolean values are passed', () => { 48 | assert.throws(() => div(false, true), TypeError); 49 | }); 50 | it('should throw an error when there is type difference', () => { 51 | assert.throws(() => div(1,'6'), TypeError); 52 | }); 53 | it('should throw an error when there is a type difference', () => { 54 | assert.throws(() => div('1',3), TypeError); 55 | }); 56 | it('should throws an error when the object is not of Complex', () => { 57 | assert.throws(() => div(a,{p: 30, r: 20}), TypeError); 58 | }); 59 | it('should throw an error when no object match', () => { 60 | assert.throws(() => div({p: 30, q: 20}, a), TypeError) 61 | }); 62 | it('should throw an error when matrix has a length difference', () => { 63 | assert.throws(() => div([[1]], [[1,2]]), TypeError); 64 | }); 65 | it('should throw an error when matrix has a type difference', () => { 66 | assert.throws(() => div([[1]], [['5']]), TypeError); 67 | }); 68 | it('should throw an error when 1D array has length difference', () => { 69 | assert.throws(() => div([1,2,3,4,5],[1,2,3,4]), TypeError); 70 | }); 71 | it('should throw an error when 1D array has type difference', () => { 72 | assert.throws(() => div([1,2,3,'4',5],[1,2,3,4,5]), TypeError); 73 | }); 74 | }); 75 | -------------------------------------------------------------------------------- /test/even-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | isEven = require('../src/even').check; 3 | 4 | describe('[Function: isEven]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof isEven, 'function'); 8 | }); 9 | 10 | it('should return a boolean when an integer is passed', () => { 11 | assert.strictEqual(typeof isEven(1), 'boolean'); 12 | }); 13 | 14 | it('should return \'true\' when \'44\' is passed', () => { 15 | assert.strictEqual(isEven(44), true); 16 | }); 17 | 18 | it('should return \'false\' when \'35\' is passed', () => { 19 | assert.strictEqual(isEven(35), false); 20 | }); 21 | 22 | it('should throw an error when a floating point is passed', () => { 23 | assert.throws(() => isEven(31.101996), TypeError); 24 | }); 25 | 26 | it('should throw an error when a string is passed', () => { 27 | assert.throws(() => isEven('31'), TypeError); 28 | }); 29 | 30 | it('should throw an error when no arguments passed', () => { 31 | assert.throws(() => isEven(), TypeError); 32 | }); 33 | 34 | }); 35 | -------------------------------------------------------------------------------- /test/factorial-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | factorial = require('../src/factorial').find; 3 | 4 | describe('[Function: factorial]', () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof factorial, 'function'); 7 | }); 8 | 9 | it('should return 120 when 5 is passed', () => { 10 | assert.strictEqual(factorial(5), 120); 11 | }); 12 | 13 | it('should return 1 when 0 is passed', () => { 14 | assert.strictEqual(factorial(0), 1); 15 | }); 16 | 17 | it('should return 1 when 1 is passed', () => { 18 | assert.strictEqual(factorial(1), 1); 19 | }); 20 | 21 | it('should throw an error when a negative number is passed', () => { 22 | assert.throws(() => factorial(-20), TypeError); 23 | }); 24 | 25 | it('should throw an error when a floating point is passed', () => { 26 | assert.throws(() => factorial(31.101996), TypeError); 27 | }); 28 | 29 | it('should throw an error when a string is passed', () => { 30 | assert.throws(() => factorial('31'), TypeError); 31 | }); 32 | 33 | it('should throw an error when no arguments passed', () => { 34 | assert.throws(() => factorial(), TypeError); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /test/fibbinary-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | fibbinary = require('../src/fibbinary').check; 3 | 4 | describe('[Function: fibbinary]', () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof fibbinary, 'function'); 7 | }); 8 | 9 | it('should return a boolean only', () => { 10 | assert.strictEqual(typeof fibbinary(100), 'boolean'); 11 | }); 12 | 13 | it('should return a boolean only', () => { 14 | assert.strictEqual(typeof fibbinary(31), 'boolean'); 15 | }); 16 | 17 | it('should return a \'true\' when 10 is passed as an argument', () => { 18 | assert.strictEqual(fibbinary(10), true); 19 | }); 20 | 21 | it('should return a \'false\' when 63 is passed as an argument', () => { 22 | assert.strictEqual(fibbinary(63), false); 23 | }); 24 | 25 | it('should throw an error when no arguments passed', () => { 26 | assert.throws(() => fibbinary(), TypeError); 27 | }); 28 | 29 | it('should throw an error when a negative number is passed', () => { 30 | assert.throws(() => fibbinary(-20), TypeError); 31 | }); 32 | 33 | it('should throw an error when a string is passed', () => { 34 | assert.throws(() => fibbinary('asd'), TypeError); 35 | }); 36 | 37 | it('should throw an error when a floating point number is passed', () => { 38 | assert.throws(() => fibbinary(1.265436), TypeError); 39 | }); 40 | }); -------------------------------------------------------------------------------- /test/fibonacci-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | fibonacci = require('../src/fibonacci').find, 3 | rangeFibonacci = require('../src/fibonacci').range; 4 | 5 | describe('[Function: fibonacci]', () => { 6 | 7 | it('should be a function', () => { 8 | assert.strictEqual(typeof fibonacci, 'function'); 9 | }); 10 | 11 | it('should return a number when a positive integer is passed', () => { 12 | assert.strictEqual(typeof fibonacci(1), 'number'); 13 | }); 14 | 15 | it('should return \'8\' when \'6\' is passed', () => { 16 | assert.strictEqual(fibonacci(6), 8); 17 | }); 18 | 19 | it('should return \'610\' when \'15\' is passed', () => { 20 | assert.strictEqual(fibonacci(15), 610); 21 | }); 22 | 23 | it('should throw an error when a negative number is passed', () => { 24 | assert.throws(() => fibonacci(-20), TypeError); 25 | }); 26 | 27 | it('should throw an error when a floating point is passed', () => { 28 | assert.throws(() => fibonacci(31.101996), TypeError); 29 | }); 30 | 31 | it('should throw an error when a string is passed', () => { 32 | assert.throws(() => fibonacci('31'), TypeError); 33 | }); 34 | 35 | it('should throw an error when no arguments passed', () => { 36 | assert.throws(() => fibonacci(), TypeError); 37 | }); 38 | 39 | }); 40 | 41 | 42 | describe('[Function: rangeFibonacci]', () => { 43 | 44 | it('should be a function', () => { 45 | assert.strictEqual(typeof rangeFibonacci, 'function'); 46 | }); 47 | 48 | it('should return `[0, 1, 1]` when 1 is passed', () => { 49 | assert.deepEqual(rangeFibonacci(1), [0, 1, 1]); 50 | }); 51 | 52 | it('should return `[0, 1, 1, 2]` when 2 is passed', () => { 53 | assert.deepEqual(rangeFibonacci(2), [0, 1, 1, 2]); 54 | }); 55 | }); 56 | -------------------------------------------------------------------------------- /test/find-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | find = require('../src/find'); 3 | 4 | describe('[Function: find]', () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof find, 'function'); 7 | }); 8 | 9 | it('should return 120 when factorial is passed as arg & 5 is passed as arg for factorial', () => { 10 | assert.strictEqual(find('factorial')(5), 120); 11 | }); 12 | 13 | it('should return 1 when factorial is passed as arg & 1 is passed as arg for factorial', () => { 14 | assert.strictEqual(find('factorial')(0), 1); 15 | }); 16 | 17 | it('should return 1 when factorial is passed as arg & 1 is passed as arg for factorial', () => { 18 | assert.strictEqual(find('factorial')(1), 1); 19 | }); 20 | 21 | it('should return 52 when bell is called with 5', () => { 22 | assert.strictEqual(find('bell')(5), 52); 23 | }); 24 | 25 | it('should return 14 when catalan is called with 4', () => { 26 | assert.strictEqual(find('catalan')(4), 14); 27 | }); 28 | 29 | it('should return 5 when fibonacci is called with 5', () => { 30 | assert.strictEqual(find('fibonacci')(5), 5); 31 | }); 32 | 33 | it('should return 25 when magic is called with 2', () => { 34 | assert.strictEqual(find('magic')(2), 25); 35 | }); 36 | 37 | it('should return 12 when padovan is called with 21', () => { 38 | assert.strictEqual(find('padovan')(12), 21); 39 | }); 40 | 41 | it('should return 30 when smart is called with 1', () => { 42 | assert.strictEqual(find('smart')(1), 30); 43 | }); 44 | 45 | it('should return 8 when triangular is called with 36', () => { 46 | assert.strictEqual(find('triangular')(8), 36); 47 | }); 48 | 49 | it('should return 8 when catalan is called with 7', () => { 50 | assert.strictEqual(find('ugly')(7), 8); 51 | }); 52 | 53 | it('should throw an error when an integer is passed as arg', () => { 54 | assert.throws(() => find(7)(12), TypeError); 55 | }); 56 | 57 | it('should throw an error when a floating-point number is passed as arg', () => { 58 | assert.throws(() => find(25.7)(5), TypeError); 59 | }); 60 | 61 | it('should throw an error when when factorial is passed as arg & negative number is passed as arg for factorial', () => { 62 | assert.throws(() => find('factorial')(-20), TypeError); 63 | }); 64 | 65 | it('should throw an error when when factorial is passed as arg & floating-point number is passed as arg for factorial', () => { 66 | assert.throws(() => find('factorial')(31.101996), TypeError); 67 | }); 68 | 69 | it('should throw an error when when factorial is passed as arg & string is passed as arg for factorial', () => { 70 | assert.throws(() => find('factorial')('31'), TypeError); 71 | }); 72 | 73 | it('should throw an error when no arguments passed', () => { 74 | assert.throws(() => find()(), TypeError); 75 | }); 76 | 77 | it('should throw an error when no arguments passed', () => { 78 | assert.throws(() => find('factorial')(), TypeError); 79 | }); 80 | 81 | it('should throw an error if sum is called', () => { 82 | assert.throws(() => find('sum')([1,2,3,4]), TypeError); 83 | }); 84 | }); 85 | -------------------------------------------------------------------------------- /test/frequency-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | frequency = require('../src/frequency/index'); 3 | 4 | describe('[Function: frequency]', () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof frequency, 'function'); 7 | }); 8 | 9 | it('should return 3 when the following array and occurance is passed', () => { 10 | assert.strictEqual(frequency([1, 2, 3, 2, 1, 2], 2), 3); 11 | }); 12 | 13 | it('should return 1 when the following string and occurance is passed', () => { 14 | assert.strictEqual(frequency('testing frequency', 'es'), 1); 15 | }); 16 | 17 | it('should return 2 when the following string and occurance is passed', () => { 18 | assert.strictEqual( 19 | frequency(['testing', 'frequency', 'testing'], 'testing'), 20 | 2 21 | ); 22 | }); 23 | 24 | it('should return 3 when the following array and occurance is passed', () => { 25 | assert.strictEqual(frequency([1, 2, 3, 1, 2, 4, 5, 1], 1), 3); 26 | }); 27 | 28 | it('should return 2 when the following array and occurance is passed', () => { 29 | assert.strictEqual(frequency(['a', 'y', 'a', 'b', 'c', 'z', 'i'], 'a'), 2); 30 | }); 31 | 32 | it('should return 1 when the following array and occurance is passed', () => { 33 | assert.strictEqual( 34 | frequency(['hello', 'world', 'lorem', 'ipsum', 'hello'], 'world'), 35 | 1 36 | ); 37 | }); 38 | 39 | it('should return 0 when the following array and occurance is passed', () => { 40 | assert.strictEqual( 41 | frequency(['hello', 5, 'lorem', 7, 'ipsum', 'hello'], 'world'), 42 | 0 43 | ); 44 | }); 45 | 46 | it('should return 2 when the following string and occurance is passed', () => { 47 | assert.strictEqual(frequency('banana', 'ana'), 2); 48 | }); 49 | 50 | it('should throw TypeError when nothing is passed as arguments', () => { 51 | assert.throws(() => frequency(), TypeError); 52 | }); 53 | 54 | it('should throw TypeError when an Objectis passed', () => { 55 | assert.throws(() => frequency({foo: 'bar'}), TypeError); 56 | }); 57 | }); 58 | -------------------------------------------------------------------------------- /test/frugal-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | frugal = require('../src/frugal').check; 3 | 4 | describe('[Function: frugal]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof frugal, 'function'); 8 | }); 9 | 10 | it('should return a boolean value when a positive integer is passed', () => { 11 | assert.strictEqual(typeof frugal(2), 'boolean'); 12 | }); 13 | 14 | it('should throw an error when a negative number is passed', () => { 15 | assert.throws(() => frugal(-20), TypeError); 16 | }); 17 | 18 | it('should throw an error when a floating point number is passed', () => { 19 | assert.throws(() => frugal(20.51513), TypeError); 20 | }); 21 | 22 | it('should throw an error when a string is passed', () => { 23 | assert.throws(() => frugal('20'), TypeError); 24 | }); 25 | 26 | it('should throw an error when no argument is passed', () => { 27 | assert.throws(() => frugal(), TypeError); 28 | }); 29 | 30 | it('should return \'true\' when \'256\' is passed ', () => { 31 | assert.strictEqual(frugal(256), true); 32 | }); 33 | 34 | it('should return \'true\' when \'343\' is passed ', () => { 35 | assert.strictEqual(frugal(343), true); 36 | }); 37 | 38 | it('should return \'false\' when \'100\' is passed ', () => { 39 | assert.strictEqual(frugal(100), false); 40 | }); 41 | 42 | it('should return \'false\' when \'129\' is passed ', () => { 43 | assert.strictEqual(frugal(129), false); 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /test/gcd-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | gcd = require('../src/gcd'); 3 | 4 | describe('[Function: gcd]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof gcd, 'function'); 8 | }); 9 | 10 | it('should return a number when an array of number is passed', () => { 11 | assert.strictEqual(typeof gcd([1, 1]), 'number'); 12 | }); 13 | 14 | it('should return \'6\' when \'[12, 18, 24]\' is passed', () => { 15 | assert.strictEqual(gcd([12, 18, 24]), 6); 16 | }); 17 | 18 | it('should return \'4\' when \'[24, 28]\' is passed', () => { 19 | assert.strictEqual(gcd([24, 28]), 4); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => gcd(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => gcd(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => gcd('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => gcd(), TypeError); 36 | }); 37 | 38 | it('should throw an error when arguments are not numbers', () => { 39 | assert.throws(() => gcd(['string', 'string']), TypeError); 40 | }); 41 | 42 | }); 43 | -------------------------------------------------------------------------------- /test/harshad-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | isHarshad = require('../src/harshad').check; 3 | 4 | describe('[Function: isHarshad]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof isHarshad, 'function'); 8 | }); 9 | 10 | it('should return a boolean value when a positive integer is passed', () => { 11 | assert.strictEqual(typeof isHarshad(1), 'boolean'); 12 | }); 13 | 14 | it('should return \'true\' when \'18\' is passed', () => { 15 | assert.strictEqual(isHarshad(18), true); 16 | }); 17 | 18 | it('should return \'false\' when \'23\' is passed', () => { 19 | assert.strictEqual(isHarshad(23), false); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => isHarshad(-10), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => isHarshad(21.2446), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => isHarshad('67'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => isHarshad(), TypeError); 36 | }); 37 | 38 | }); 39 | -------------------------------------------------------------------------------- /test/hexagonal-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | hexagonal = require('../src/hexagonal').find; 3 | 4 | describe('[Function: hexagonal]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof hexagonal, 'function'); 8 | }); 9 | 10 | it('should return a number when a positive integer is passed', () => { 11 | assert.strictEqual(typeof hexagonal(1), 'number'); 12 | }); 13 | 14 | it('should return \'66\' when \'6\' is passed', () => { 15 | assert.strictEqual(hexagonal(6), 66); 16 | }); 17 | 18 | it('should return \'496\' when \'15\' is passed', () => { 19 | assert.strictEqual(hexagonal(15), 435); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => hexagonal(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => hexagonal(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => hexagonal('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => hexagonal(), TypeError); 36 | }); 37 | 38 | }); -------------------------------------------------------------------------------- /test/hoax-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | hoax = require('../src/hoax').check; 3 | 4 | describe('[Function: hoax]', () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof hoax, 'function'); 7 | }); 8 | 9 | it('should throw TypeError when an string is passed as an argument', () => { 10 | assert.throws(() => hoax('string'), TypeError); 11 | }); 12 | 13 | it('should return `true` when 84 is passed as an argument', () => { 14 | assert.strictEqual(hoax(84), true); 15 | }); 16 | 17 | it('should return `false` when 15 is passed as an argument', () => { 18 | assert.strictEqual(hoax(15), false); 19 | }); 20 | 21 | it('should return `false` when 3 is passed as an argument', () => { 22 | assert.strictEqual(hoax(3), false); 23 | }); 24 | 25 | it('should return `false` when 16 is passed as an argument', () => { 26 | assert.strictEqual(hoax(16), false); 27 | }); 28 | 29 | it('should return `false` when 19 is passed as an argument', () => { 30 | assert.strictEqual(hoax(19), false); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /test/index-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | M = require('../src'); 3 | 4 | describe('[Object: mathball]', () => { 5 | it('should return an object', () => { 6 | assert.equal(typeof M, 'object'); 7 | }); 8 | }); 9 | -------------------------------------------------------------------------------- /test/inverse-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | modInv = require('../src/inverse'); 3 | 4 | describe('[Function: modInv]', () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof modInv, 'function'); 7 | }); 8 | it('should return 4 when 3 and 11 is passed', () => { 9 | assert.strictEqual(modInv(3,11), 4); 10 | }); 11 | it('should return 0 when numbers passed are not relatively prime like 6 and 9',() => { 12 | assert.strictEqual(modInv(6,9), 0); 13 | }); 14 | it('should return 12 when 10 and 17 is passed', () => { 15 | assert.strictEqual(modInv(10,17), 12); 16 | }); 17 | it('should throw an error when negative number(s) is passed', () => { 18 | assert.throws(() => modInv(-20,10), TypeError); 19 | }); 20 | it('should throw an error when a floating point is passed', () => { 21 | assert.throws(() => modInv(2.4, 3.5) , TypeError); 22 | }); 23 | it('should throw an error when a string is passed', () => { 24 | assert.throws(() => modInv('10','17'), TypeError); 25 | }); 26 | it('should throw an error when no arguments are passed', () => { 27 | assert.throws(() => modInv(), TypeError); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/kaprekar-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"), 2 | isKaprekar = require("../src/kaprekar").check; 3 | 4 | describe("[Function: isKaprekar]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof isKaprekar, "function"); 7 | }); 8 | 9 | it("should return a boolean value when a positive integer is passed", () => { 10 | assert.strictEqual(typeof isKaprekar(1), "boolean"); 11 | }); 12 | 13 | it("should return 'true' when '9' is passed", () => { 14 | assert.strictEqual(isKaprekar(9), true); 15 | }); 16 | 17 | it("should return 'false' when '44' is passed", () => { 18 | assert.strictEqual(isKaprekar(44), false); 19 | }); 20 | 21 | it("should throw an error when a negative number is passed", () => { 22 | assert.throws(() => isKaprekar(-20), TypeError); 23 | }); 24 | 25 | it("should throw an error when a floating point is passed", () => { 26 | assert.throws(() => isKaprekar(31.101996), TypeError); 27 | }); 28 | 29 | it("should throw an error when a string is passed", () => { 30 | assert.throws(() => isKaprekar("31"), TypeError); 31 | }); 32 | 33 | it("should throw an error when no arguments passed", () => { 34 | assert.throws(() => isKaprekar(), TypeError); 35 | }); 36 | 37 | it("should 'continue' loop execution instead of throwing error", () => { 38 | assert.doesNotThrow(() => isKaprekar(10), Error); 39 | }); 40 | }); 41 | -------------------------------------------------------------------------------- /test/lcm-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | lcm = require('../src/lcm'); 3 | 4 | describe('[Function: lcm]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof lcm, 'function'); 8 | }); 9 | 10 | it('should return a number when an array of number is passed', () => { 11 | assert.strictEqual(typeof lcm([1, 1]), 'number'); 12 | }); 13 | 14 | it('should return \'30\' when \'[2, 3, 5, 15]\' is passed', () => { 15 | assert.strictEqual(lcm([2, 3, 5, 15]), 30); 16 | }); 17 | 18 | it('should throw an error when a negative number is passed', () => { 19 | assert.throws(() => lcm(-20), TypeError); 20 | }); 21 | 22 | it('should throw an error when a floating point is passed', () => { 23 | assert.throws(() => lcm(31.101996), TypeError); 24 | }); 25 | 26 | it('should throw an error when a string is passed', () => { 27 | assert.throws(() => lcm('31'), TypeError); 28 | }); 29 | 30 | it('should throw an error when no arguments passed', () => { 31 | assert.throws(() => lcm(), TypeError); 32 | }); 33 | 34 | }); 35 | -------------------------------------------------------------------------------- /test/length-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | length = require('../src/length'); 3 | 4 | describe('[Function: length]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof length, 'function'); 8 | }); 9 | 10 | it('should return a number when a number is passed', () => { 11 | assert.strictEqual(typeof length(1), 'number'); 12 | }); 13 | 14 | it('should return a number when a string is passed', () => { 15 | assert.strictEqual(typeof length('hello'), 'number'); 16 | }); 17 | 18 | it('should return a number when an array is passed', () => { 19 | assert.strictEqual(typeof length([1, 2, 3]), 'number'); 20 | }); 21 | 22 | it('should return a number when an object is passed', () => { 23 | assert.strictEqual(typeof length({x: 0, y: 0}), 'number'); 24 | }); 25 | 26 | it('should return \'4\' when the number \'1745\' is passed', () => { 27 | assert.strictEqual(length(1745), 4); 28 | }); 29 | 30 | it('should return \'6\' when the string \'bright\' is passed', () => { 31 | assert.strictEqual(length('bright'), 6); 32 | }); 33 | 34 | it('should return \'3\' when the array \'[4, 5, 6]\' is passed', () => { 35 | assert.strictEqual(length([4, 5, 6]), 3); 36 | }); 37 | 38 | it('should return \'0\' when the object \'{}\' is passed', () => { 39 | assert.strictEqual(length({}), 0); 40 | }); 41 | 42 | it('should throw a \'TypeError\' when a \'Bool\' is passed', () => { 43 | assert.throws(() => length(true), TypeError); 44 | }); 45 | 46 | it('should return \'5\' when the number \'124.34\' is passed', () => { 47 | assert.strictEqual(length(124.34), 5); 48 | }); 49 | 50 | it('should throw an error when no value is passed', () => { 51 | assert.throws(() => length(), TypeError); 52 | }); 53 | 54 | }); 55 | -------------------------------------------------------------------------------- /test/lucky-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"), 2 | isLucky = require("../src/lucky").check; 3 | 4 | describe("[Function: isLucky]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof isLucky, "function"); 7 | }); 8 | 9 | it("should return a boolean value when a positive integer is passed", () => { 10 | assert.strictEqual(typeof isLucky(1), "boolean"); 11 | }); 12 | 13 | it("should return 'true' when '13' is passed", () => { 14 | assert.strictEqual(isLucky(13), true); 15 | }); 16 | 17 | it("should return 'false' when '2' is passed", () => { 18 | assert.strictEqual(isLucky(2), false); 19 | }); 20 | 21 | it("should throw an error when a negative number is passed", () => { 22 | assert.throws(() => isLucky(-20), TypeError); 23 | }); 24 | 25 | it("should throw an error when a floating point is passed", () => { 26 | assert.throws(() => isLucky(31.101996), TypeError); 27 | }); 28 | 29 | it("should throw an error when a string is passed", () => { 30 | assert.throws(() => isLucky("31"), TypeError); 31 | }); 32 | 33 | it("should throw an error when no arguments passed", () => { 34 | assert.throws(() => isLucky(), TypeError); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /test/magic-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | magic = require('../src/magic').find; 3 | 4 | describe('[Function: magic]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof magic, 'function'); 8 | }); 9 | 10 | it('should return a number when a positive integer is passed', () => { 11 | assert.strictEqual(typeof magic(1), 'number'); 12 | }); 13 | 14 | it('should return \'125\' when \'4\' is passed', () => { 15 | assert.strictEqual(magic(4), 125); 16 | }); 17 | 18 | it('should return \'750\' when \'12\' is passed', () => { 19 | assert.strictEqual(magic(12), 750); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => magic(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => magic(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => magic('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => magic(), TypeError); 36 | }); 37 | 38 | }); 39 | -------------------------------------------------------------------------------- /test/matrixChain-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | matrixChain = require('../src/matrixChain').matrixChain; 3 | 4 | describe('[Function: matrixChain]', () => { 5 | assert.strictEqual(typeof matrixChain, 'function'); 6 | 7 | it('should return 4500 when [10,30,5,60] is passed', () => { 8 | assert.strictEqual(matrixChain([10,30,5,60]), 4500); 9 | }); 10 | 11 | it('should return 14000 when [40,20,30,10] is passed', () => { 12 | assert.strictEqual(matrixChain([40,20,30,10]), 14000); 13 | }); 14 | 15 | it('should return 78 when [2,3,5,2,4,3] is passed', () => { 16 | assert.strictEqual(matrixChain([2,3,5,2,4,3]), 78); 17 | }); 18 | 19 | it('should throw a TypeError when just an integer is passed', () => { 20 | assert.throws(() => matrixChain(20), TypeError); 21 | }); 22 | 23 | it('should throw a TypeError when a negative number is passed in the array', () => { 24 | assert.throws(() => matrixChain([10,-20, -40,20]), TypeError); 25 | }); 26 | 27 | it('should throw an error for string input', () => { 28 | assert.throws(() => matrixChain(['10','20','30']), TypeError); 29 | }); 30 | 31 | it('should not accept a floating point', () => { 32 | assert.throws(() => matrixChain([1.3, 4.5, 5.4, 6.7]), TypeError); 33 | }); 34 | 35 | it('should not accept a boolean', () => { 36 | assert.throws(() => matrixChain([true, false, true, false]), TypeError); 37 | }); 38 | }); 39 | -------------------------------------------------------------------------------- /test/matrixExponentiation-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | matrixExpo = require('../src/matrixExponentiation').matrixExpo; 3 | 4 | describe('[Funtion: matrixExpo]',() => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof matrixExpo, 'function'); 8 | }); 9 | 10 | it('should return an error if the matrix is not a square matrix', () => { 11 | assert.strictEqual(matrixExpo([[1,2], [3]], 2), "Not a square matrix"); 12 | }); 13 | 14 | it('should return an error if the power is not a positive integer', () => { 15 | assert.throws(() => matrixExpo([[1,2], [3,4]], -2), TypeError); 16 | }); 17 | 18 | it('should return [[ 37, 54 ], [ 81, 118 ]] when ([[1,2], [3,4]], 3) is passed as arguments', () => { 19 | assert.deepEqual(matrixExpo([[1,2], [3,4]], 3), [[ 37, 54 ], [ 81, 118 ]] ); 20 | }); 21 | 22 | it('should return [[ 7, 10 ], [ 15, 22 ]] when ([[1,2], [3,4]], 2) is passed as arguments', () => { 23 | assert.deepEqual(matrixExpo([[1,2], [3,4]], 2), [[ 7, 10 ], [ 15, 22 ]] ); 24 | }); 25 | }); -------------------------------------------------------------------------------- /test/max-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | max = require('../src/max'); 3 | 4 | describe('[Function: max]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof max, 'function'); 8 | }); 9 | 10 | it('should return a number when an array of number is passed', () => { 11 | assert.strictEqual(typeof max([1, 1]), 'number'); 12 | }); 13 | 14 | it('should return \'72\' when \'[45, -5, 72, 10]\' is passed', () => { 15 | assert.strictEqual(max([45, -5, 72, 10]), 72); 16 | }); 17 | 18 | it('should return \'42\' when \'[-10, 0, 41, 42]\' is passed', () => { 19 | assert.strictEqual(max([-10, 0, 41, 42]), 42); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => max(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => max(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => max('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when a boolean is passed', () => { 35 | assert.throws(() => max(true), TypeError); 36 | }); 37 | 38 | it('should throw an error when no arguments passed', () => { 39 | assert.throws(() => max(), TypeError); 40 | }); 41 | 42 | it('should throw an error when arguments are not number', () => { 43 | assert.throws(() => max(['hi', 'hello']), TypeError); 44 | }); 45 | 46 | }); 47 | -------------------------------------------------------------------------------- /test/median-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | median = require('../src/median'); 3 | 4 | describe('[Function: median]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof median, 'function'); 8 | }); 9 | 10 | it('should return a number when an array of number is passed', () => { 11 | assert.strictEqual(typeof median([1, 1]), 'number'); 12 | }); 13 | 14 | it('should return \'12.57\' when \'[12.57, 5.23, 24]\' is passed', () => { 15 | assert.strictEqual(median([12.57, 5.23, 24]), 12.57); 16 | }); 17 | 18 | it('should return \'0\' when \'[-2, -1, 1, 2]\' is passed', () => { 19 | assert.strictEqual(median([-2, -1, 1, 2]), 0); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => median(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => median(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => median('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => median(), TypeError); 36 | }); 37 | 38 | }); 39 | -------------------------------------------------------------------------------- /test/min-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | min = require('../src/min'); 3 | 4 | describe('[Function: min]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof min, 'function'); 8 | }); 9 | 10 | it('should return a number when an array of number is passed', () => { 11 | assert.strictEqual(typeof min([1, 1]), 'number'); 12 | }); 13 | 14 | it('should return \'-5.4\' when \'[-5.4, -5, 2, 10]\' is passed', () => { 15 | assert.strictEqual(min([-5.4, -5, 2, 10]), -5.4); 16 | }); 17 | 18 | it('should return \'0\' when \'[1, 0, 3, 22]\' is passed', () => { 19 | assert.strictEqual(min([1, 0, 3, 22]), 0); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => min(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => min(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => min('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when a boolean is passed', () => { 35 | assert.throws(() => min(true), TypeError); 36 | }); 37 | 38 | it('should throw an error when no arguments passed', () => { 39 | assert.throws(() => min(), TypeError); 40 | }); 41 | 42 | }); 43 | -------------------------------------------------------------------------------- /test/multiply-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | mul = require('../src/multiply'), 3 | Complex = require('../src/complex'), 4 | a = new Complex(7,3), 5 | b = new Complex(4,6); 6 | 7 | describe('[Function: mul]', () => { 8 | it('should return a function', () => { 9 | assert.strictEqual(typeof mul, 'function'); 10 | }); 11 | it('should throw a TypeError if no parameter is passed', () => { 12 | assert.throws(() => mul(), TypeError); 13 | }); 14 | it('should throw a TypeError when one parameter is passed', () => { 15 | assert.throws(() => mul([1,2,3,4]), TypeError); 16 | }); 17 | it('should return 2 when (1,2) is passed', () => { 18 | assert.strictEqual(mul(1,2), 2); 19 | }); 20 | it('should return 0.24 when (1.2, 0.2) is passed', () => { 21 | assert.strictEqual(mul(1.2,0.2), 0.24); 22 | }); 23 | it('should throw an error when (1,2,4) is passed', () => { 24 | assert.throws(() => mul(1,2,4), TypeError); 25 | }); 26 | it('should return "2" when strings (1,2) is passed', () => { 27 | assert.strictEqual(mul('1','2'), "2"); 28 | }); 29 | it('should return [72,54] when [12,9],[6,6] is passed', () => { 30 | assert.deepStrictEqual(mul([12,9],[6,6]), [72,54]); 31 | }); 32 | it('should return [[4,4],[10,8]] when ([[1,2],[3,4]],[[2,0],[1,2]]) is passed', () => { 33 | assert.deepStrictEqual(mul([[1,2],[3,4]], [[2,0],[1,2]]), [[4,4],[10,8]]); 34 | }); 35 | it('should return an object when Complex objects a,b are passed', () => { 36 | assert.deepStrictEqual(mul(a,b), {re: 28, im: 18}); 37 | }); 38 | it('should throw an error when boolean values are passed', () => { 39 | assert.throws(() => mul(false, true), TypeError); 40 | }); 41 | it('should throw an error when there is type difference', () => { 42 | assert.throws(() => mul(1,'6'), TypeError); 43 | }); 44 | it('should throw an error when there is a type difference', () => { 45 | assert.throws(() => mul('1',3), TypeError); 46 | }); 47 | it('should throws an error when the object is not of Complex', () => { 48 | assert.throws(() => mul(a,{p: 30, r: 20}), TypeError); 49 | }); 50 | it('should throw an error when no object match', () => { 51 | assert.throws(() => mul({p: 30, q: 20}, a), TypeError) 52 | }); 53 | it('should throw an error when matrix has a length difference', () => { 54 | assert.throws(() => mul([[1]], [[1,2]]), TypeError); 55 | }); 56 | it('should throw an error when matrix has a type difference', () => { 57 | assert.throws(() => mul([[1]], [['5']]), TypeError); 58 | }); 59 | it('should throw an error when 1D array has length difference', () => { 60 | assert.throws(() => mul([1,2,3,4,5],[1,2,3,4]), TypeError); 61 | }); 62 | it('should throw an error when 1D array has type difference', () => { 63 | assert.throws(() => mul([1,2,3,'4',5],[1,2,3,4,5]), TypeError); 64 | }); 65 | it('should return -80 when -8, 10 is passed', () => { 66 | assert.strictEqual(mul(-8,10), -80); 67 | }); 68 | }); 69 | -------------------------------------------------------------------------------- /test/neon-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | neon = require('../src/neon').check; 3 | 4 | describe('[Function: neon]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof neon, 'function'); 8 | }); 9 | 10 | it('should return a boolean value when a positive integer is passed', () => { 11 | assert.strictEqual(typeof neon(1), 'boolean'); 12 | }); 13 | 14 | it('should return \'true\' when \'9\' is passed', () => { 15 | assert.strictEqual(neon(9), true); 16 | }); 17 | 18 | it('should return \'false\' when \'7\' is passed', () => { 19 | assert.strictEqual(neon(7), false); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => neon(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => neon(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => neon('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => neon(), TypeError); 36 | }); 37 | 38 | }); 39 | -------------------------------------------------------------------------------- /test/odd-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | isOdd = require('../src/odd').check; 3 | 4 | describe('[Function: isOdd]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof isOdd, 'function'); 8 | }); 9 | 10 | it('should return a boolean when an integer is passed', () => { 11 | assert.strictEqual(typeof isOdd(1), 'boolean'); 12 | }); 13 | 14 | it('should return \'true\' when \'19\' is passed', () => { 15 | assert.strictEqual(isOdd(19), true); 16 | }); 17 | 18 | it('should return \'false\' when \'56\' is passed', () => { 19 | assert.strictEqual(isOdd(56), false); 20 | }); 21 | 22 | it('should throw an error when a floating point is passed', () => { 23 | assert.throws(() => isOdd(31.101996), TypeError); 24 | }); 25 | 26 | it('should throw an error when a string is passed', () => { 27 | assert.throws(() => isOdd('31'), TypeError); 28 | }); 29 | 30 | it('should throw an error when no arguments passed', () => { 31 | assert.throws(() => isOdd(), TypeError); 32 | }); 33 | 34 | }); 35 | -------------------------------------------------------------------------------- /test/padovan-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"), 2 | padovan = require("../src/padovan").find; 3 | 4 | describe("[Function: padovan]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof padovan, "function"); 7 | }); 8 | 9 | it("should return a number when a positive integer is passed", () => { 10 | assert.strictEqual(typeof padovan(1), "number"); 11 | }); 12 | 13 | it("should return '4' when '6' is passed", () => { 14 | assert.strictEqual(padovan(6), 4); 15 | }); 16 | 17 | it("should return '49' when '15' is passed", () => { 18 | assert.strictEqual(padovan(15), 49); 19 | }); 20 | 21 | it("should throw an error when a negative number is passed", () => { 22 | assert.throws(() => padovan(-20), TypeError); 23 | }); 24 | 25 | it("should throw an error when a floating point is passed", () => { 26 | assert.throws(() => padovan(31.101996), TypeError); 27 | }); 28 | 29 | it("should throw an error when a string is passed", () => { 30 | assert.throws(() => padovan("31"), TypeError); 31 | }); 32 | 33 | it("should throw an error when no arguments passed", () => { 34 | assert.throws(() => padovan(), TypeError); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /test/palindrome-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | palindrome = require('../src/palindrome').check, 3 | rangePalindrome = require('../src/palindrome').range; 4 | 5 | describe('[Function: palindrome]', () => { 6 | 7 | it('should be a function', () => { 8 | assert.strictEqual(typeof palindrome, 'function'); 9 | }); 10 | 11 | it('should return a boolean value when a non-negative integer is passed', () => { 12 | assert.strictEqual(typeof palindrome(1), 'boolean'); 13 | }); 14 | 15 | it('should return \'true\' when \'141\' is passed', () => { 16 | assert.strictEqual(palindrome(141), true); 17 | }); 18 | 19 | it('should return \'false\' when \'726\' is passed', () => { 20 | assert.strictEqual(palindrome(726), false); 21 | }); 22 | 23 | it('should throw an error when a negative number is passed', () => { 24 | assert.throws(() => palindrome(-20), TypeError); 25 | }); 26 | 27 | it('should throw an error when a floating point is passed', () => { 28 | assert.throws(() => palindrome(31.101996), TypeError); 29 | }); 30 | 31 | it('should throw an error when a string is passed', () => { 32 | assert.throws(() => palindrome('31'), TypeError); 33 | }); 34 | 35 | it('should throw an error when no arguments passed', () => { 36 | assert.throws(() => palindrome(), TypeError); 37 | }); 38 | 39 | }); 40 | 41 | describe('[Function: rangePalindrome]', () => { 42 | 43 | it('should be a function', () => { 44 | assert.strictEqual(typeof rangePalindrome, 'function'); 45 | }); 46 | 47 | it('should return a object when a non-negative integers are passed', () => { 48 | assert.strictEqual(typeof rangePalindrome(1,5), 'object'); 49 | }); 50 | 51 | it('should throw an error when a negative number is passed', () => { 52 | assert.throws(() => rangePalindrome(-20), TypeError); 53 | }); 54 | 55 | it('should throw an error when a lower limit is larger than upper limit', () => { 56 | assert.throws(() => rangePalindrome(25,20), TypeError); 57 | }); 58 | 59 | it('should throw an error when a negative number is passed', () => { 60 | assert.throws(() => rangePalindrome(15,-20), TypeError); 61 | }); 62 | 63 | it('should throw an error when a floating point is passed', () => { 64 | assert.throws(() => rangePalindrome(31.101996), TypeError); 65 | }); 66 | 67 | it('should throw an error when a string is passed', () => { 68 | assert.throws(() => rangePalindrome('31'), TypeError); 69 | }); 70 | 71 | it('should throw an error when no arguments passed', () => { 72 | assert.throws(() => rangePalindrome(), TypeError); 73 | }); 74 | 75 | it('should throw an error when one argument is passed', () => { 76 | assert.throws(() => rangePalindrome(5), TypeError); 77 | }); 78 | 79 | it('should return \'[0,1,2,3,4,5,6,7,8,9,11]\' when \'0, 18\' are passed', () => { 80 | assert.deepEqual(rangePalindrome(0,18), [0,1,2,3,4,5,6,7,8,9,11]); 81 | }); 82 | 83 | }); 84 | -------------------------------------------------------------------------------- /test/perfect-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | perfect = require('../src/perfect').check; 3 | 4 | describe('[Function: perfect]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof perfect, 'function'); 8 | }); 9 | 10 | it('should return a boolean value when a positive integer is passed', () => { 11 | assert.strictEqual(typeof perfect(1), 'boolean'); 12 | }); 13 | 14 | it('should return \'true\' when \'496\' is passed', () => { 15 | assert.strictEqual(perfect(496), true); 16 | }); 17 | 18 | it('should return \'false\' when \'34\' is passed', () => { 19 | assert.strictEqual(perfect(34), false); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => perfect(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => perfect(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => perfect('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => perfect(), TypeError); 36 | }); 37 | 38 | }); 39 | -------------------------------------------------------------------------------- /test/performance-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | performance = require('../src/performance'); 3 | 4 | describe('[Function: performance]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof performance, 'function'); 8 | }); 9 | 10 | it('should return \'undefined\'', () => { 11 | assert.strictEqual(performance(Math.pow, 2, 3), undefined); 12 | }); 13 | 14 | it('should return \'undefined\'', () => { 15 | assert.strictEqual(performance(Math.sqrt, 4), undefined); 16 | }); 17 | 18 | it('should throw an error when a negative number is passed', () => { 19 | assert.throws(() => performance(-20), TypeError); 20 | }); 21 | 22 | it('should throw an error when a floating point is passed', () => { 23 | assert.throws(() => performance(31.101996), TypeError); 24 | }); 25 | 26 | it('should throw an error when a string is passed', () => { 27 | assert.throws(() => performance('31'), TypeError); 28 | }); 29 | 30 | it('should throw an error when no arguments passed', () => { 31 | assert.throws(() => performance(), TypeError); 32 | }); 33 | 34 | it('should reuturn \'undefined\'', () => { 35 | assert.strictEqual(performance(Math.sqrt), undefined); 36 | }); 37 | 38 | it('should return \'undefined\'', () => { 39 | assert.strictEqual(performance(Math.pow, 1, 2, 3), undefined); 40 | }); 41 | }); 42 | -------------------------------------------------------------------------------- /test/permute-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | permute = require('../src/permute'); 3 | 4 | describe("[Function: permute]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof permute, "function"); 7 | }); 8 | 9 | it("should return '4' when '4' is passed", () => { 10 | assert.deepEqual(permute('4'), ['4']); 11 | }); 12 | 13 | it("should return 'a' when 'a' is passed", () => { 14 | assert.deepEqual(permute("a"), ["a"]); 15 | }); 16 | 17 | it("should return '15' and '51' when '15' is passed", () => { 18 | assert.deepEqual(permute("15"), ["15","51"]); 19 | }); 20 | 21 | it("should return 'ab' and 'ba' when 'ab' is passed", () => { 22 | assert.deepEqual(permute("ab"), ["ab","ba"]); 23 | }); 24 | 25 | it("should return 'abc', 'acb', 'bac', 'bca', 'cab' and 'cba' when 'abc' is passed", () => { 26 | assert.deepEqual(permute("abc"), ["abc","acb","bac","bca","cab","cba"]); 27 | }); 28 | 29 | it("should return '123', '132', '213', '231', '312' and '321' when '123' is passed", () => { 30 | assert.deepEqual(permute("123"), ["123","132","213","231","312","321"]); 31 | }); 32 | 33 | it("should throw an error when a positive number is passed", () => { 34 | assert.throws(() => permute(123), TypeError); 35 | }); 36 | 37 | it("should throw an error when a negative number is passed", () => { 38 | assert.throws(() => permute(-20), TypeError); 39 | }); 40 | 41 | it("should throw an error when a floating point is passed", () => { 42 | assert.throws(() => permute(31.101996), TypeError); 43 | }); 44 | 45 | it("should throw an error when no arguments passed", () => { 46 | assert.throws(() => permute(), TypeError); 47 | }); 48 | }); 49 | -------------------------------------------------------------------------------- /test/pipe-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | pipe = require('../src/pipe'); 3 | 4 | describe('[Function: pipe]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof pipe, 'function'); 8 | }); 9 | 10 | it('should return an \'object\' when 42 is passed without done()', () => { 11 | assert.strictEqual(typeof pipe(42), 'object'); 12 | }); 13 | 14 | it('should return 42 when 42 is passed and done() gets called', () => { 15 | assert.strictEqual(pipe(42).done(), 42) 16 | }); 17 | 18 | it('should return 50 when 42 is passed and add(8), done() gets called', () => { 19 | assert.strictEqual(pipe(42).add(8).done(), 50) 20 | }); 21 | 22 | it('should return 40 when 42 is passed and sub(2), done() gets called', () => { 23 | assert.strictEqual(pipe(42).sub(2).done(), 40) 24 | }); 25 | 26 | it('should return 84 when 42 is passed and mul(2), done() gets called', () => { 27 | assert.strictEqual(pipe(42).mul(2).done(), 84) 28 | }); 29 | 30 | it('should return 21 when 42 is passed and div(2), done() gets called', () => { 31 | assert.strictEqual(pipe(42).div(2).done(), 21) 32 | }); 33 | 34 | it('should return 42 when -42 is passed and abs(), done() gets called', () => { 35 | assert.strictEqual(pipe(-42).abs().done(), 42) 36 | }); 37 | 38 | it('should return 42 when [8, 23, 2, 9] is passed and abs(), done() gets called', () => { 39 | assert.strictEqual(pipe([8, 23, 2, 9]).sum().done(), 42) 40 | }); 41 | 42 | it('should throw an error when no arguments passed', () => { 43 | assert.throws(() => pipe(), TypeError); 44 | }); 45 | 46 | }); 47 | -------------------------------------------------------------------------------- /test/popcount-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | popcount = require('../src/popcount'); 3 | 4 | describe('[Function: popcount]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof popcount, 'function'); 8 | }); 9 | 10 | it('should return a number when a positive integer is passed', () => { 11 | assert.strictEqual(typeof popcount(1), 'number'); 12 | }); 13 | 14 | it('should return \'2\' when \'6\' is passed', () => { 15 | assert.strictEqual(popcount(6), 2); 16 | }); 17 | 18 | it('should return \'4\' when \'15\' is passed', () => { 19 | assert.strictEqual(popcount(15), 4); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => popcount(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => popcount(27.996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => popcount('47'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => popcount(), TypeError); 36 | }); 37 | 38 | }); -------------------------------------------------------------------------------- /test/pow-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | pow = require("../src/pow"); 3 | 4 | describe('[Function: pow]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof pow, 'function'); 8 | }); 9 | 10 | it('should return a number when two numbers are passed', () => { 11 | assert.strictEqual(typeof pow(11, 1), 'number'); 12 | }); 13 | 14 | it('should return a number when three numbers are passed', () => { 15 | assert.strictEqual(typeof pow(11, 1, 2), 'number'); 16 | }); 17 | 18 | it('should return \'1\' when \'6, 3, 5\' is passed', () => { 19 | assert.strictEqual(pow(6, 3, 5), 1); 20 | }); 21 | 22 | it('should return \'1\' when \'8, 4, 5\' is passed', () => { 23 | assert.strictEqual(pow(8, 4, 5), 1); 24 | }); 25 | 26 | it('should return \'125\' when \'5, 3\' is passed', () => { 27 | assert.strictEqual(pow(5, 3), 125); 28 | }); 29 | 30 | it('should throw an error when a negative number is passed as modulo', () => { 31 | assert.throws(() => pow(20, 5, -9), TypeError); 32 | }); 33 | 34 | it('should throw an error when a negative number is passed', () => { 35 | assert.throws(() => pow(-20), TypeError); 36 | }); 37 | 38 | it('should throw an error when a floating point is passed', () => { 39 | assert.throws(() => pow(31.101996), TypeError); 40 | }); 41 | 42 | it('should throw an error when a string is passed', () => { 43 | assert.throws(() => pow('31'), TypeError); 44 | }); 45 | 46 | it('should throw an error when no arguments passed', () => { 47 | assert.throws(() => pow(), TypeError); 48 | }); 49 | 50 | }); -------------------------------------------------------------------------------- /test/prime-factor-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | findPrimeFactors = require('../src/prime-factor').find; 3 | 4 | describe("[Function: findPrimeFactors]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof findPrimeFactors, "function"); 7 | }); 8 | 9 | it("should return '[2,5]' when '100' is passed", () => { 10 | assert.deepEqual(findPrimeFactors(100), [2, 2, 5, 5]); 11 | }); 12 | 13 | it("should return '[101]' when '100' is passed", () => { 14 | assert.deepEqual(findPrimeFactors(101), [101]); 15 | }); 16 | 17 | it("should throw an error when a negative number is passed", () => { 18 | assert.throws(() => findPrimeFactors(-20), TypeError); 19 | }); 20 | 21 | it("should throw an error when a floating point is passed", () => { 22 | assert.throws(() => findPrimeFactors(31.101996), TypeError); 23 | }); 24 | 25 | it("should throw an error when a string is passed", () => { 26 | assert.throws(() => findPrimeFactors("31"), TypeError); 27 | }); 28 | 29 | it("should throw an error when no arguments passed", () => { 30 | assert.throws(() => findPrimeFactors(), TypeError); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /test/priority-queue-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | PriorityQueue = require('../src/priority-queue'), 3 | PQ = new PriorityQueue(); 4 | 5 | describe('[Function: priorityQueue]', () => { 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof PriorityQueue, 'function'); 8 | }); 9 | 10 | it(`should return a string when 'queue()' method is called`, () => { 11 | assert.strictEqual(typeof PQ.queue(), 'string'); 12 | }); 13 | 14 | it(`should throw TypeError when a string is passed as argument in 'push()'`, () => { 15 | assert.throws(() => PQ.push('string'), TypeError); 16 | }); 17 | 18 | it(`should return 'true' when a number is passed as argument in 'push()'`, () => { 19 | assert.strictEqual(PQ.push(20), true); 20 | }); 21 | 22 | it(`should return '20' when 'pop()' is called, as there is '20' available in the queue`, () => { 23 | assert.strictEqual(PQ.pop(), 20); 24 | }); 25 | 26 | it(`should return 'Queue Underflow' when 'pop()' is called, as the queue is empty`, () => { 27 | assert.strictEqual(PQ.pop(), 'Queue Underflow'); 28 | }); 29 | 30 | it(`should return 'Queue Underflow' when 'rear()' is called, as the queue is empty`, () => { 31 | assert.strictEqual(PQ.rear(), 'Queue Underflow'); 32 | }); 33 | 34 | it(`should return 'Queue Underflow' when 'front()' is called, as the queue is empty`, () => { 35 | assert.strictEqual(PQ.front(), 'Queue Underflow'); 36 | }); 37 | 38 | it(`should return 'Queue Underflow' when 'front()' is called, as the queue is empty`, () => { 39 | assert.strictEqual(PQ.rear(), 'Queue Underflow'); 40 | }); 41 | 42 | it(`should return 'true' when 'empty()' is called, as the queue is empty`, () => { 43 | assert.strictEqual(PQ.empty(), true); 44 | }); 45 | 46 | it(`should return 'true' when 'push()' is called with a number as an arg`, () => { 47 | assert.strictEqual(PQ.push(20), true); 48 | }); 49 | 50 | it(`should return 'true' when 'push()' is called with a number as an arg`, () => { 51 | assert.strictEqual(PQ.push(10), true); 52 | }); 53 | 54 | it(`should return 'true' when 'push()' is called with a number as an arg`, () => { 55 | assert.strictEqual(PQ.push(30), true); 56 | }); 57 | 58 | it(`should not return 'Queue Underflow' when 'front()' is called, as queue is not empty`, () => { 59 | assert.notStrictEqual(PQ.front(), 'Queue Underflow'); 60 | }); 61 | 62 | it(`should not return 'Queue Underflow' when 'rear()' is called, as queue is not empty`, () => { 63 | assert.notStrictEqual(PQ.rear(), 'Queue Underflow'); 64 | }); 65 | 66 | it(`should not return 'false' when 'empty()' is called, as queue is not empty`, () => { 67 | assert.strictEqual(PQ.empty(), false); 68 | }); 69 | 70 | it(`should return 'number' when 'size()' is called`, () => { 71 | assert.strictEqual(typeof PQ.size(), 'number'); 72 | }); 73 | }); 74 | -------------------------------------------------------------------------------- /test/range-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | range = require('../src/range'), 3 | rangePrime = range('prime'), 4 | rangePalindrome = range('palindrome'); 5 | 6 | 7 | 8 | describe('[Function: range]', () => { 9 | 10 | it('should be a function', () => { 11 | assert.strictEqual(typeof range, 'function'); 12 | }); 13 | 14 | it('should return `[0, 1, 1]` when fibonacci is passed as arg & 1 is passed as arg to fibonacci', () => { 15 | assert.deepEqual(range('fibonacci')(1), [0, 1, 1]); 16 | }); 17 | 18 | it('should return `[0, 1, 1, 2]` when fibonacci is passed as arg & 2 is passed as arg to fibonacci', () => { 19 | assert.deepEqual(range('fibonacci')(2), [0, 1, 1, 2]); 20 | }); 21 | 22 | it('should return [2, 3, 5, 7, 11, 13, 17, 19] when 20 is passed to rangePrime', () => { 23 | assert.deepStrictEqual(rangePrime(20), [2, 3, 5, 7, 11, 13, 17, 19]); 24 | }); 25 | 26 | it('should return [55,66,77,88,99] when 50, 100 is passed to rangePalindrome', () => { 27 | assert.deepStrictEqual(rangePalindrome(50,100), [55,66,77,88,99]); 28 | }); 29 | 30 | it('should throw an error when no arguments passed', () => { 31 | assert.throws(() => range(), TypeError); 32 | }); 33 | 34 | it('should throw an error when a number is passed', () => { 35 | assert.throws(() => range(31), TypeError); 36 | }); 37 | 38 | it('should throw an error when a negative number is passed', () => { 39 | assert.throws(() => range(-20), TypeError); 40 | }); 41 | 42 | it('should throw an error when a floating point is passed', () => { 43 | assert.throws(() => range(31.101996), TypeError); 44 | }); 45 | 46 | it('should throw an error when sum is called', () => { 47 | assert.throws(() => range('sum'), TypeError); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /test/smart-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"), 2 | smart = require("../src/smart").find; 3 | 4 | describe("[Function: smart]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof smart, "function"); 7 | }); 8 | 9 | it("should return an integer value when a positive integer is passed", () => { 10 | assert.strictEqual(typeof smart(1), "number"); 11 | }); 12 | 13 | it("should return '273' when '50' is passed", () => { 14 | assert.strictEqual(smart(50), 273); 15 | }); 16 | 17 | it("should throw an error when a negative number is passed", () => { 18 | assert.throws(() => smart(-20), TypeError); 19 | }); 20 | 21 | it("should throw an error when a floating point is passed", () => { 22 | assert.throws(() => smart(31.101996), TypeError); 23 | }); 24 | 25 | it("should throw an error when a string is passed", () => { 26 | assert.throws(() => smart("31"), TypeError); 27 | }); 28 | 29 | it("should throw an error when no arguments passed", () => { 30 | assert.throws(() => smart(), TypeError); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /test/smith-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"), 2 | smith = require("../src/smith").check; 3 | 4 | describe("[Function: smith]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof smith, "function"); 7 | }); 8 | 9 | it("should return a boolean value when a positive integer is passed", () => { 10 | assert.strictEqual(typeof smith(1), "boolean"); 11 | }); 12 | 13 | it("should return 'true' when '483' is passed", () => { 14 | assert.strictEqual(smith(483), true); 15 | }); 16 | 17 | it("should return 'false' when '34' is passed", () => { 18 | assert.strictEqual(smith(34), false); 19 | }); 20 | 21 | it("should throw an error when a negative number is passed", () => { 22 | assert.throws(() => smith(-20), TypeError); 23 | }); 24 | 25 | it("should throw an error when a floating point is passed", () => { 26 | assert.throws(() => smith(31.101996), TypeError); 27 | }); 28 | 29 | it("should throw an error when a string is passed", () => { 30 | assert.throws(() => smith("31"), TypeError); 31 | }); 32 | 33 | it("should throw an error when no arguments passed", () => { 34 | assert.throws(() => smith(), TypeError); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /test/sort-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | sort = require('../src/sort'); 3 | 4 | describe('[Function: sort]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof sort, 'function'); 8 | }); 9 | 10 | it('should return an array when an array of number is passed', () => { 11 | assert.strictEqual(typeof sort([1, 1]), 'object'); 12 | assert.strictEqual(sort([1, 1]).hasOwnProperty('length'), true); 13 | }); 14 | 15 | it('should return \'[0, 2, 4, 5, 8, 13]\' when \'[4, 5, 13, 2, 8, 0]\' is passed', () => { 16 | assert.deepStrictEqual(sort([4, 5, 13, 2, 8, 0]), [0, 2, 4, 5, 8, 13]); 17 | }); 18 | 19 | it('should return \'[13, 8, 5, 4, 2, 0]\' when \'[4, 5, 13, 2, 8, 0] & false\' is passed', () => { 20 | assert.deepStrictEqual(sort([4, 5, 13, 2, 8, 0], false), [13, 8, 5, 4, 2, 0]); 21 | }); 22 | 23 | it('should throw an error when a negative number is passed', () => { 24 | assert.throws(() => sort(-20), TypeError); 25 | }); 26 | 27 | it('should throw an error when a floating point is passed', () => { 28 | assert.throws(() => sort(31.101996), TypeError); 29 | }); 30 | 31 | it('should throw an error when a string is passed', () => { 32 | assert.throws(() => sort('31'), TypeError); 33 | }); 34 | 35 | it('should throw an error when no arguments passed', () => { 36 | assert.throws(() => sort(), TypeError); 37 | }); 38 | 39 | it('should throw an error when `order` is not boolean', () => { 40 | assert.throws(() => sort([], 'hey yoo'), TypeError); 41 | }); 42 | }); -------------------------------------------------------------------------------- /test/stack-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | Stack = require('../src/stack/'), 3 | sample = new Stack(); 4 | 5 | describe('[Function: Stack]', () => { 6 | 7 | sample.push(10); 8 | sample.push(20); 9 | sample.push(30); 10 | 11 | it('should be a function', () => { 12 | assert.strictEqual(typeof Stack, 'function'); 13 | }); 14 | 15 | it(`should return '30' when 'head()' is called`, () => { 16 | assert.strictEqual(sample.head(), 30); 17 | }); 18 | 19 | it(`should return 'false' when 'isEmpty()' is called`, () => { 20 | assert.strictEqual(sample.isEmpty(), false); 21 | }); 22 | 23 | it(`should return 'number' when 'size()' is called`, () => { 24 | assert.strictEqual(typeof sample.size(), 'number'); 25 | }); 26 | 27 | it(`should return '3' when 'size()' is called`, () => { 28 | assert.strictEqual(sample.size(), 3); 29 | }); 30 | 31 | it(`should throw TypeError when a string is passed as argument in 'push()'`, () => { 32 | assert.throws(() => sample.push('string'), TypeError); 33 | }); 34 | 35 | it(`should return 'object' type when 'copy()' is called`, () => { 36 | assert.strictEqual(typeof sample.copy(), 'object'); 37 | }); 38 | 39 | it(`should return '30 20 10' type when 'display()' is called`, () => { 40 | assert.strictEqual(sample.display(), '30 20 10'); 41 | }); 42 | 43 | it(`should return '30' when 'pop()' is called`, () => { 44 | assert.strictEqual(sample.pop(), 30); 45 | }); 46 | 47 | it(`should return '20' when 'pop()' is called`, () => { 48 | assert.strictEqual(sample.pop(), 20); 49 | }); 50 | 51 | it(`should return '10' when 'pop()' is called`, () => { 52 | assert.strictEqual(sample.pop(), 10); 53 | }); 54 | 55 | it(`should return 'Stack Underflow' when 'pop()' is called`, () => { 56 | assert.strictEqual(sample.pop(), 'Stack Underflow'); 57 | }); 58 | 59 | 60 | it(`should return 'Empty Stack' when 'head()' is called`, () => { 61 | assert.strictEqual(sample.head(), 'Empty Stack'); 62 | }); 63 | 64 | it(`should return 'true' when 'isEmpty()' is called`, () => { 65 | assert.strictEqual(sample.isEmpty(), true); 66 | }); 67 | 68 | }); 69 | -------------------------------------------------------------------------------- /test/stormer-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | stormer = require('../src/stormer').check; 3 | 4 | describe('[Function: stormer', () => { 5 | 6 | it("should be a function", () => { 7 | assert.strictEqual(typeof stormer, "function"); 8 | }); 9 | 10 | it("should throw an error when a string is passed", () => { 11 | assert.throws(() => stormer("31"), TypeError); 12 | }); 13 | 14 | it("should throw an error when a boolean is passed", () => { 15 | assert.throws(() => stormer(true), TypeError); 16 | }); 17 | 18 | it("should throw an error when a non-negative number is passed", () => { 19 | assert.throws(() => stormer(-31), TypeError); 20 | }); 21 | 22 | it("should throw an error when no arguments passed", () => { 23 | assert.throws(() => stormer(), TypeError); 24 | }); 25 | 26 | it("should return a boolean", () => { 27 | assert.strictEqual(typeof stormer(5), "boolean"); 28 | }); 29 | 30 | it("should return 'true' when '11' is passed", () => { 31 | assert.strictEqual(stormer(11), true); 32 | }); 33 | 34 | it("should return 'true' when '1' is passed", () => { 35 | assert.strictEqual(stormer(1), true); 36 | }); 37 | 38 | it("should return 'true' when '6' is passed", () => { 39 | assert.strictEqual(stormer(6), true); 40 | }); 41 | 42 | it("should return 'false' when '7' is passed", () => { 43 | assert.strictEqual(stormer(7), false); 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /test/subtract-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | sub = require('../src/subtract'), 3 | Complex = require('../src/complex'), 4 | a = new Complex(7,3), 5 | b = new Complex(4,6); 6 | 7 | describe('[Function: sub]', () => { 8 | it('should return a function', () => { 9 | assert.strictEqual(typeof sub, 'function'); 10 | }); 11 | it('should throw a TypeError if no parameter is passed', () => { 12 | assert.throws(() => sub(), TypeError); 13 | }); 14 | it('should throw a TypeError when one parameter is passed', () => { 15 | assert.throws(() => sub([1,2,3,4]), TypeError); 16 | }); 17 | it('should return 1 when (1,2) is passed', () => { 18 | assert.strictEqual(sub(1,2), -1); 19 | }); 20 | it('should return 1.0 when (1.2, 0.2) is passed', () => { 21 | assert.strictEqual(sub(1.2,0.2), 1.0); 22 | }); 23 | it('should throw an error when (1,2,4) is passed', () => { 24 | assert.throws(() => sub(1,2,4), TypeError); 25 | }); 26 | it('should return -1 when strings (1,2) is passed', () => { 27 | assert.strictEqual(sub('1','2'), "-1"); 28 | }); 29 | it('should return [6,3] when [12,9],[6,6] is passed', () => { 30 | assert.deepStrictEqual(sub([12,9],[6,6]), [6,3]); 31 | }); 32 | it('should return [[3]] when ([[8]],[[5]]) is passed', () => { 33 | assert.deepStrictEqual(sub([[8]], [[5]]), [[3]]); 34 | }); 35 | it('should return [[1,2]] when [[3,4]],[[2,2]] is passed', () => { 36 | assert.deepStrictEqual(sub([[3,4]],[[2,2]]), [[1,2]]); 37 | }); 38 | it('should return an object when Complex objects a,b are passed', () => { 39 | assert.deepStrictEqual(sub(a,b), {re: 3, im: -3}); 40 | }); 41 | it('should throw an error when boolean values are passed', () => { 42 | assert.throws(() => sub(false, true), TypeError); 43 | }); 44 | it('should throw an error when there is type difference', () => { 45 | assert.throws(() => sub(1,'6'), TypeError); 46 | }); 47 | it('should throw an error when there is a type difference', () => { 48 | assert.throws(() => sub('1',3), TypeError); 49 | }); 50 | it('should throws an error when the object is not of Complex', () => { 51 | assert.throws(() => sub(a,{p: 30, r: 20}), TypeError); 52 | }); 53 | it('should throw an error when no object match', () => { 54 | assert.throws(() => sub({p: 30, q: 20}, a), TypeError) 55 | }); 56 | it('should throw an error when matrix has a length difference', () => { 57 | assert.throws(() => sub([[1]], [[1,2]]), TypeError); 58 | }); 59 | it('should throw an error when matrix has a type difference', () => { 60 | assert.throws(() => sub([[1]], [['5']]), TypeError); 61 | }); 62 | it('should throw an error when 1D array has length difference', () => { 63 | assert.throws(() => sub([1,2,3,4,5],[1,2,3,4]), TypeError); 64 | }); 65 | it('should throw an error when 1D array has type difference', () => { 66 | assert.throws(() => sub([1,2,3,'4',5],[1,2,3,4,5]), TypeError); 67 | }); 68 | }); 69 | -------------------------------------------------------------------------------- /test/sum-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | sum = require('../src/sum'); 3 | 4 | describe('[Function: sum]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof sum, 'function'); 8 | }); 9 | 10 | it('should return a number when an array of number is passed', () => { 11 | assert.strictEqual(typeof sum([1, 1]), 'number'); 12 | }); 13 | 14 | it('should return \'41.8\' when \'[12.57, 5.23, 24]\' is passed', () => { 15 | assert.strictEqual(sum([12.57, 5.23, 24]), 41.8); 16 | }); 17 | 18 | it('should return \'0\' when \'[-2, -1, 1, 2]\' is passed', () => { 19 | assert.strictEqual(sum([-2, -1, 1, 2]), 0); 20 | }); 21 | 22 | it('should throw an error when a negative number is passed', () => { 23 | assert.throws(() => sum(-20), TypeError); 24 | }); 25 | 26 | it('should throw an error when a floating point is passed', () => { 27 | assert.throws(() => sum(31.101996), TypeError); 28 | }); 29 | 30 | it('should throw an error when a string is passed', () => { 31 | assert.throws(() => sum('31'), TypeError); 32 | }); 33 | 34 | it('should throw an error when no arguments passed', () => { 35 | assert.throws(() => sum(), TypeError); 36 | }); 37 | 38 | }); 39 | -------------------------------------------------------------------------------- /test/totient-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | phi = require('../src/totient'); 3 | 4 | describe("[Function: phi]", () => { 5 | it("should be a function", () => { 6 | assert.strictEqual(typeof phi, "function"); 7 | }); 8 | 9 | it('should return a number when a number is passed', () => { 10 | assert.strictEqual(typeof phi(20), 'number'); 11 | }); 12 | 13 | it("should return '6' when '9' is passed", () => { 14 | assert.strictEqual(phi(9), 6); 15 | }); 16 | 17 | it("should return '0' when '0' is passed", () => { 18 | assert.strictEqual(phi(0), 0); 19 | }); 20 | 21 | it("should return '1' when '1' is passed", () => { 22 | assert.strictEqual(phi(1), 1); 23 | }); 24 | 25 | it("should throw an error when a negative number is passed", () => { 26 | assert.throws(() => phi(-20), TypeError); 27 | }); 28 | 29 | it("should throw an error when a floating point is passed", () => { 30 | assert.throws(() => phi(31.101996), TypeError); 31 | }); 32 | 33 | it("should throw an error when a string is passed", () => { 34 | assert.throws(() => phi("31"), TypeError); 35 | }); 36 | 37 | it('should throw an error when a boolean is passed', () => { 38 | assert.throws(() => phi(true), TypeError); 39 | }); 40 | 41 | it("should throw an error when no arguments passed", () => { 42 | assert.throws(() => phi(), TypeError); 43 | }); 44 | }); 45 | -------------------------------------------------------------------------------- /test/triangular-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | triangular = require('../src/triangular').find; 3 | 4 | describe('[Function: triangular]', () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof triangular, 'function'); 7 | }); 8 | 9 | it('should return 36 when 8 is passed', () => { 10 | assert.strictEqual(triangular(8), 36); 11 | }); 12 | 13 | it('should throw an error when a boolean is passed', () => { 14 | assert.throws(() => triangular(true), TypeError); 15 | }); 16 | 17 | it('should throw an error if a string is passed', () => { 18 | assert.throws(() => triangular('hello'), TypeError); 19 | }); 20 | 21 | it('should throw an error when a negative number is passed', () => { 22 | assert.throws(() => triangular(-4), TypeError); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /test/ugly-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'); 2 | const ugly = require('../src/ugly').find; 3 | 4 | describe('[Function: ugly]', () => { 5 | it('should be a function', () => { 6 | assert.strictEqual(typeof ugly, 'function'); 7 | }); 8 | 9 | it('should return 5832 when 150 is passed as arg', () => { 10 | assert.strictEqual(ugly(150), 5832); 11 | }); 12 | 13 | it('should throw TypeError when a negative number is passed', () => { 14 | assert.throws(() => ugly(-10), TypeError); 15 | }); 16 | 17 | it('should throw TypeError when a string is passed', () => { 18 | assert.throws(() => ugly('string'), TypeError); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /test/union-spec.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'), 2 | union = require('../src/union'); 3 | 4 | describe('[Function: union]', () => { 5 | 6 | it('should be a function', () => { 7 | assert.strictEqual(typeof union, 'function'); 8 | }); 9 | 10 | it('should throw an error when a non-array argument is passed', () => { 11 | assert.throws(() => union('ssad', [1,2,34]), TypeError); 12 | }); 13 | 14 | it('should throw an error when a non-array argument is passed', () => { 15 | assert.throws(() => union([1,2,34], 'ssad'), TypeError); 16 | }); 17 | 18 | it('should return "[1, 2, 3, 4]" when "[1, 2, 3]" and "[2, 3, 4]" are passed', () => { 19 | assert.deepEqual(union([2, 3, 4], [1, 2, 3]), [1, 2, 3, 4]); 20 | }); 21 | 22 | it('should return "[1, 45, -10]" when "[1, 2, 3, 4]" and "[-10, 1, 2, 3, 4, 45]" are passed', () => { 23 | assert.deepEqual(union([1, 45, -10], [1, 2, 3, 4]), [-10, 1, 2, 3, 4, 45]); 24 | }); 25 | 26 | it('should return "[1, 2, 3, 4]" when "[1, 45, -10]" and "[-10, 1, 2, 3, 4, 45]" are passed', () => { 27 | assert.deepEqual(union([1, 2, 3, 4], [1, 45, -10]), [-10, 1, 2, 3, 4, 45]); 28 | }); 29 | 30 | it('should return "[1]" when "[1, 45, -10]" and "[-10, 1, 45]" are passed', () => { 31 | assert.deepEqual(union([1, 45, -10], [1]), [-10, 1, 45]); 32 | }); 33 | }); --------------------------------------------------------------------------------