Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
factorial
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Evgeny
factorial
Commits
6eb50899
Commit
6eb50899
authored
Jul 17, 2015
by
missingdays
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create factorial function and add all tests
parent
ace46aba
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
2 deletions
+71
-2
my_factorial.js
src/my_factorial.js
+29
-2
main_test.js
test/main_test.js
+42
-0
No files found.
src/my_factorial.js
View file @
6eb50899
...
...
@@ -5,8 +5,35 @@
* Distributed under terms of the MIT license.
*/
function
factorial
()
{
return
1
;
function
factorial
(
n
)
{
var
e
=
new
Error
(
'Argument should be a positive number'
)
if
(
!
isNumeric
(
n
))
{
throw
e
;
}
if
(
!
isInt
(
n
))
{
throw
e
;
}
if
(
n
<
0
)
{
throw
e
;
}
var
value
=
1
;
for
(
var
i
=
2
;
i
<=
n
;
i
++
)
{
value
*=
i
;
}
return
value
;
}
function
isNumeric
(
n
)
{
return
!
isNaN
(
parseFloat
(
n
))
&&
isFinite
(
n
);
}
function
isInt
(
n
){
return
Number
(
n
)
===
n
&&
n
%
1
===
0
;
}
module
.
exports
=
factorial
;
test/main_test.js
View file @
6eb50899
...
...
@@ -10,7 +10,49 @@ var my_factorial = require('../src/my_factorial');
exports
.
testCalculatesCorrectly
=
function
(
test
){
test
.
equal
(
1
,
my_factorial
(
0
));
test
.
equal
(
1
,
my_factorial
(
1
));
test
.
equal
(
2
,
my_factorial
(
2
));
test
.
equal
(
6
,
my_factorial
(
3
));
test
.
equal
(
120
,
my_factorial
(
5
));
test
.
done
();
}
exports
.
testCalculatesCorrectlyForBigNumbers
=
function
(
test
)
{
test
.
equal
(
355687428096000
,
my_factorial
(
17
));
test
.
equal
(
620448401733239439360000
,
my_factorial
(
24
));
test
.
equal
(
15511210043330985984000000
,
my_factorial
(
25
));
test
.
done
();
}
exports
.
testThrowsWithIncorrectArgument
=
function
(
test
)
{
test
.
throws
(
function
()
{
my_factorial
(
"10"
);
},
Error
,
"Fail for non-numeric argument"
);
test
.
throws
(
function
()
{
my_factorial
(
2.5
);
},
Error
,
"Fail for float argument"
);
test
.
throws
(
function
()
{
my_factorial
(
-
3
);
},
Error
,
"Fail for negative argument"
);
test
.
done
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment