How to resolve the algorithm Factorial step by step in the Jsish programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Factorial step by step in the Jsish programming language

Table of Contents

Problem Statement

Write a function to return the factorial of a number. Solutions can be iterative or recursive. Support for trapping negative   n   errors is optional.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Factorial step by step in the Jsish programming language

Source code in the jsish programming language

/* Factorial, in Jsish */

/* recursive */
function fact(n) { return ((n < 2) ? 1 : n * fact(n - 1)); }

/* iterative */
function factorial(n:number) {
    if (n < 0) throw format("factorial undefined for negative values: %d", n);

    var fac = 1;
    while (n > 1) fac *= n--;
    return fac;
}

if (Interp.conf('unitTest') > 0) {
;fact(18);
;fact(1);

;factorial(18);
;factorial(42);
try { factorial(-1); } catch (err) { puts(err); }
}

  

You may also check:How to resolve the algorithm Terminal control/Dimensions step by step in the Julia programming language
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the Go programming language
You may also check:How to resolve the algorithm Dynamic variable names step by step in the Lingo programming language
You may also check:How to resolve the algorithm Apply a digital filter (direct form II transposed) step by step in the Haskell programming language
You may also check:How to resolve the algorithm Boolean values step by step in the SPL programming language