-
Code const factorial = (n, f = 1) => {do {f *= n||1} while(n -= Math.sign(n)); return f};
Test Cases const chai = require("chai"); const assert = chai.assert; chai.config.truncateThreshold=0; describe("factorial", function() { it("should work with positive", function() { assert.strictEqual(factorial( 1 ), 1); assert.strictEqual(factorial( 2 ), 2); assert.strictEqual(factorial( 3 ), 6); assert.strictEqual(factorial( 4 ), 24); assert.strictEqual(factorial( 5 ), 120); assert.strictEqual(factorial( 6 ), 720); assert.strictEqual(factorial( 7 ), 5040); assert.strictEqual(factorial( 8 ), 40320); assert.strictEqual(factorial( 9 ), 362880); assert.strictEqual(factorial( 10 ), 3628800); }); it("should work with negative", function() { assert.strictEqual(factorial( -1 ), -1); assert.strictEqual(factorial( -2 ), 2); assert.strictEqual(factorial( -3 ), -6); assert.strictEqual(factorial( -4 ), 24); assert.strictEqual(factorial( -5 ), -120); assert.strictEqual(factorial( -6 ), 720); }); it("should work with zero", function() { assert.strictEqual(factorial(0), 1); }); });
Output:
-
Code const factorial = (n, f = 1) => {do {f *= n||1} while(n = n - Math.sign(n)); return f};- const factorial = (n, f = 1) => {do {f *= n||1} while(n -= Math.sign(n)); return f};
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}