Move History

Fork Selected
  • 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);
      });
    });
  • 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};