- Factorials
- Permutations
- Combinations
- Node.js is required to run the library
- Mocha is used for testing
-
factorials.js - modules check input is positive integers and 0.
- getFactorial (n)
- returns n! for valid n
- getPartialPerm (n, k)
- returns partial permutation for valid n, k
- combination (n, k)
- returns n choose k for valid n,k
// factorials.js exports module.exports = { getFactorial: getFactorial, getPartialPerm: getPartialPerm, combination: combination }
- import into your file
// import modules and use const facts = require('./factorials'); let x = facts.getFactorial(5); // x = 120 x = facts.getPartialPerm(5, 3); // x = 60 x = facts.combination(5, 3); // x = 10
- getFactorial (n)
-
useFactorials.js - example code to use the library
- run from the command line with input
$ node useFactorials.js 5 3
- output
factorial of 5 is: 120 partial permutation of 5,3 is: 60 combination of 5,3 is: 10
- test.js - contains the tests.
// Multiple ways to run test.js $ mocha $ mocha test $ mocha --reporter=nyan // for the rainbow space cat nyan $ npm test
- learn unit testing
- learn Mocha
- learn Node.js
- Tania Rascia's Unit Testing in JavaScript with Mocha
- Betsy Stevens