Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit61c9e8b

Browse files
fix: factorial function (TheAlgorithms#1093)
1 parent6f55ed4 commit61c9e8b

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

‎Maths/Factorial.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ const calcRange = (num) => {
1919

2020
constcalcFactorial=(num)=>{
2121
if(num===0){
22-
return'The factorial of 0 is 1.'
22+
return1
2323
}
2424
if(num<0){
25-
return'Sorry, factorial does not exist for negative numbers.'
25+
throwError('Sorry, factorial does not exist for negative numbers.')
2626
}
2727
if(!num){
28-
return'Sorry, factorial does not exist for null or undefined numbers.'
28+
throwError('Sorry, factorial does not exist for null or undefined numbers.')
2929
}
3030
if(num>0){
3131
constrange=calcRange(num)
3232
constfactorial=range.reduce((a,c)=>a*c,1)
33-
return`Thefactorial of${num} is${factorial}`
33+
returnfactorial
3434
}
3535
}
3636

‎Maths/test/Factorial.test.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,20 @@ import { calcFactorial } from '../Factorial'
22

33
describe('calcFactorial',()=>{
44
it('should return a statement for value "0"',()=>{
5-
expect(calcFactorial(0)).toBe('The factorial of 0 is 1.')
5+
expect(calcFactorial(0)).toBe(1)
66
})
77

8-
it('should return a statement for "null" and "undefined"',()=>{
9-
constnullFactorial=calcFactorial(null)
10-
constundefinedFactorial=calcFactorial(undefined)
11-
12-
expect(nullFactorial).toBe(
13-
'Sorry, factorial does not exist for null or undefined numbers.'
14-
)
15-
expect(undefinedFactorial).toBe(
16-
'Sorry, factorial does not exist for null or undefined numbers.'
17-
)
8+
it('should throw error for "null" and "undefined"',()=>{
9+
expect(()=>{calcFactorial(null)}).toThrow(Error)
10+
expect(()=>{calcFactorial(undefined)}).toThrow(Error)
1811
})
1912

20-
it('should not support negative numbers',()=>{
21-
constnegativeFactorial=calcFactorial(-5)
22-
expect(negativeFactorial).toBe(
23-
'Sorry, factorial does not exist for negative numbers.'
24-
)
13+
it('should throw error for negative numbers',()=>{
14+
expect(()=>{calcFactorial(-1)}).toThrow(Error)
2515
})
2616

2717
it('should return the factorial of a positive number',()=>{
2818
constpositiveFactorial=calcFactorial(3)
29-
expect(positiveFactorial).toBe('The factorial of 3 is 6')
19+
expect(positiveFactorial).toBe(6)
3020
})
3121
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp