In this article we will discuss recursive functions in JavaScript. The recursive is a function that calls itself by looping there must be a break otherwise it will create infinite loops.
Example:
<script type="text/javascript">
var n = Number(prompt("Please enter a number for factorial", ""));
alert(factorial(n));
function factorial(n) {
if (n == 0 || n == 1) {
return 1;
}
var result = n;
while (n > 1) {
result = result * (n - 1)
n =n - 1;
}
return result;
}
</script>
Output:
Post your comments / questions
Recent Article
- How to enable Search Feature in Django admin?
- How to check PAN-Aadhaar is Linked or NOT?
- How to customize pagination for django admin?
- How to fix HAXM is not installed |in Android Studio
- How to fix CMOS Checksum Error in Computer or Laptop | SOLVED
- Reactivating windows after a Hardware change on PC or Laptop
- FIXED: Windows reported that the hardware of your device has changed. Error code :0xc004F211
- "redirect" is not defined pylance("reportUndefinedVariable)
Related Article