Don't wanna be here? Send us removal request.
Text
Finding factorial using Ternary operator.
Hello, everyone....
I'm going to briefly discuss a code in JavaScript, which was invoked to find the factorial of a number.
starting with the keyword const, const is used to define a variable as fixed, in general, we are allocating a fixed location of memory to the variable. 'factorial' is the name of our variable (variables are used to store our data just like a container).
Now we created a function using the keyword 'function' followed by the function name 'fac' and in parentheses, there comes the parameter (typically the values we give our functions to perform operations on)
The body of the function is all we need to focus on for now, 'return' is to return the value. ternary operator (syntax -> condition? true: false) checks if n is less than 2. If n is less than 2 1 will be the return value, else n*fac(n-1). Here comes the recursive functional call, for every n, greater than 2 the n is multiplied by itself and a factional call of the previous value of n.
so, it evaluates to 1*2*3 = 6. This is how recursion works and the final value is calculated by backtracking the tree. The last line of code is to print the value on the console.
Thank You <3!
0 notes