Course
Introduction
Print Hello WorldAdd Two NumbersFind the Square RootCalculate the Area of a TriangleSwap Two VariablesConvert Kilometers to MilesConvert Celsius to FahrenheitWork With ConstantsWrite to ConsoleControl Flow
Solve Quadratic EquationCheck if a number is Positive, Negative, or ZeroCheck if a Number is Odd or EvenFind the Largest Among Three NumbersCheck Prime NumberPrint All Prime Numbers in an IntervalFind the Factorial of a NumberDisplay the Multiplication TablePrint the Fibonacci SequenceCheck Armstrong NumberFind Armstrong Number in an IntervalMake a Simple CalculatorFind the Sum of Natural NumbersCheck if the Numbers Have Same Last DigitFind HCF or GCDFind LCMFind the Factors of a NumberDisplay Fibonacci Sequence Using RecursionFunctions
Generate a Random NumberFind Sum of Natural Numbers Using RecursionGuess a Random NumberFind Factorial of Number Using RecursionConvert Decimal to BinaryFind ASCII Value of CharacterSet a Default Parameter Value For a FunctionCheck If a Variable is of Function TypePass Parameter to a setTimeout() FunctionPerform Function OverloadingPass a Function as ParameterArrays and Objects
Shuffle Deck of CardsCreate Objects in Different WaysRemove a Property from an ObjectCheck if a Key Exists in an ObjectClone a JS ObjectLoop Through an ObjectMerge Property of Two ObjectsCount the Number of Keys/Properties in an ObjectAdd Key/Value Pair to an ObjectConvert Objects to StringsReplace all Instances of a Character in a StringRemove Specific Item From an ArrayCheck if An Array Contains a Specified ValueInsert Item in an ArrayAppend an Object to an ArrayCheck if An Object is An ArrayEmpty an ArrayAdd Element to Start of an ArrayRemove Duplicates From ArrayMerge Two Arrays and Remove Duplicate ItemsSort Array of Objects by Property ValuesCreate Two Dimensional ArrayExtract Given Property Values from Objects as ArrayCompare Elements of Two ArraysGet Random Item From an ArrayPerform Intersection Between Two ArraysSplit Array into Smaller ChunksCheck If A Variable Is undefined or nullIllustrate Different Set OperationsStrings
Check Whether a String is Palindrome or NotSort Words in Alphabetical OrderReplace Characters of a StringReverse a StringCheck the Number of Occurrences of a Character in the StringConvert the First Letter of a String into UpperCaseCount the Number of Vowels in a StringCheck Whether a String Starts and Ends With Certain CharactersReplace All Occurrences of a StringCreate Multiline StringsFormat Numbers as Currency StringsGenerate Random StringCheck if a String Starts With Another StringTrim a StringCheck Whether a String Contains a SubstringCompare Two StringsEncode a String to Base64Replace All Line Breaks withGet File ExtensionGenerate a Range of Numbers and CharactersRemove All Whitespaces From a TextMiscellaneous
Display Date and TimeCheck Leap YearFormat the DateDisplay Current DateCompare The Value of Two DatesCreate Countdown TimerInclude a JS file in Another JS fileGenerate a Random Number Between Two NumbersGet The Current URLValidate An Email AddressImplement a StackImplement a QueueCheck if a Number is Float or IntegerGet the Dimensions of an ImageConvert Date to NumberJavaScript Program to Find Factorial of Number Using Recursion
To understand this example, you should have the knowledge of the following JavaScript programming topics:
The factorial of a number is the product of all the numbers from 1 to that number. For example,
factorial of 5 is equal to 1 * 2 * 3 * 4 * 5 = 120.
The factorial of a positive number n is given by:
factorial of n (n!) = 1 * 2 * 3 * 4.....n
The factorial of negative numbers do not exist and the factorial of 0 is 1.
Example: Find Factorial Using Recursion
// program to find the factorial of a number
function factorial(x) {
// if number is 0
if (x == 0) {
return 1;
}
// if number is positive
else {
return x * factorial(x - 1);
}
}
// take input from the user
const num = prompt('Enter a positive number: ');
// calling factorial() if num is positive
if (num >= 0) {
const result = factorial(num);
console.log(`The factorial of ${num} is ${result}`);
}
else {
console.log('Enter a positive number.');
}
Output
Enter a positive number: 4
The factorial of 4 is 24
In the above program, the user is prompted to enter a number.
When the user enters a negative number, a message Enter a positive number. is shown.
When the user enters a positive number or 0, the function factorial(num) gets called.
- If the user enters the number 0, the program will return 1.
- If the user enters a number greater than 0, the program will recursively call itself by decreasing the number.
- This process continues until the number becomes 1. Then when the number reaches 0, 1 is returned.
Here,
factorial(4) returns 4 * factorial(3)
factorial(3) returns 4 * 3 * factorial(2)
factorial(2) returns 4 * 3 * 2 * factorial(1)
factorial(1) returns 4 * 3 * 2 * 1 * factorial(0)
factorial(0) returns 4 * 3 * 2 * 1 * 1
Also Read: