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

fix: factorial function#1093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletionsMaths/Factorial.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,18 +19,18 @@ const calcRange = (num) => {

const calcFactorial = (num) => {
if (num === 0) {
return'The factorial of 0 is 1.'
return1
}
if (num < 0) {
return'Sorry, factorial does not exist for negative numbers.'
throw Error('Sorry, factorial does not exist for negative numbers.')
}
if (!num) {
return'Sorry, factorial does not exist for null or undefined numbers.'
throw Error('Sorry, factorial does not exist for null or undefined numbers.')
}
if (num > 0) {
const range = calcRange(num)
const factorial = range.reduce((a, c) => a * c, 1)
return`Thefactorial of ${num} is ${factorial}`
return factorial
}
}

Expand Down
24 changes: 7 additions & 17 deletionsMaths/test/Factorial.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,30 +2,20 @@ import { calcFactorial } from '../Factorial'

describe('calcFactorial', () => {
it('should return a statement for value "0"', () => {
expect(calcFactorial(0)).toBe('The factorial of 0 is 1.')
expect(calcFactorial(0)).toBe(1)
})

it('should return a statement for "null" and "undefined"', () => {
const nullFactorial = calcFactorial(null)
const undefinedFactorial = calcFactorial(undefined)

expect(nullFactorial).toBe(
'Sorry, factorial does not exist for null or undefined numbers.'
)
expect(undefinedFactorial).toBe(
'Sorry, factorial does not exist for null or undefined numbers.'
)
it('should throw error for "null" and "undefined"', () => {
expect(() => { calcFactorial(null) }).toThrow(Error)
expect(() => { calcFactorial(undefined) }).toThrow(Error)
})

it('should not support negative numbers', () => {
const negativeFactorial = calcFactorial(-5)
expect(negativeFactorial).toBe(
'Sorry, factorial does not exist for negative numbers.'
)
it('should throw error for negative numbers', () => {
expect(() => { calcFactorial(-1) }).toThrow(Error)
})

it('should return the factorial of a positive number', () => {
const positiveFactorial = calcFactorial(3)
expect(positiveFactorial).toBe('The factorial of 3 is 6')
expect(positiveFactorial).toBe(6)
})
})

[8]ページ先頭

©2009-2025 Movatter.jp