Skip to content

Commit 20d4b7d

Browse files
committed
Added factorial to coding challenges.
1 parent 9c565e5 commit 20d4b7d

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

factorial/factorial.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/***************************************************************
2+
* *
3+
* CodePrep.io *
4+
* *
5+
* Function: factorial(num) *
6+
* Input: 1 number parameter *
7+
* Output: number *
8+
* *
9+
* Output expectations: *
10+
* var myTest = factorial(4); *
11+
* console.log(myTest); // 24 *
12+
* *
13+
* Factorial is the product of an integer and all the *
14+
* integers below it. For example the factorial of 4 is *
15+
* 24 which is 4 * 3 * 2 * 1. *
16+
* Write the code for the factorial function that *
17+
* takes a number as input and then outputs a number. *
18+
* *
19+
**************************************************************/
20+
21+
var factorial = function(num) {
22+
/* YOUR CODE GOES HERE */
23+
24+
}

factorial/factorial.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var should = require('should');
2+
var vm = require('vm');
3+
var fs = require('fs');
4+
var filename = __filename.replace(/\.test\.js$/, '.js');
5+
vm.runInThisContext(fs.readFileSync(filename), filename);
6+
7+
describe('Factorial', function() {
8+
it('should exist', function(){
9+
should.exist(factorial);
10+
});
11+
12+
it('should be a function', function() {
13+
factorial.should.be.a.Function;
14+
});
15+
16+
it('should return a number', function() {
17+
var result = factorial(4);
18+
should.exist(result);
19+
result.should.be.an.instanceof(Number);
20+
});
21+
22+
it('should return 1 for factorial of 1', function(){
23+
var result = factorial(1);
24+
result.should.be.eql(1);
25+
});
26+
27+
it('should return 6 for factorial of 3', function(){
28+
var result = factorial(3);
29+
result.should.be.eql(6);
30+
});
31+
32+
it('should return 362880 for factorial of 9', function(){
33+
var result = factorial(9);
34+
result.should.be.eql(362880);
35+
});
36+
37+
});

factorial/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "CodePrep.io_coding_challenges",
3+
"author": "Jennifer Bland",
4+
"url": "http://www.codeprep.io",
5+
"version": "1.0.0",
6+
"description": "CodePrep.io presents coding challenges you might face when interviewing for a Full-Stack Developer position",
7+
"scripts": {
8+
"test": "node ../node_modules/mocha/bin/mocha ./*.test.js"
9+
}
10+
}

0 commit comments

Comments
 (0)