Home > Article > Web Front-end > JavaScript uses recursive algorithm to calculate factorial example_javascript skills
The example in this article describes how JavaScript uses recursive algorithm to calculate factorial. Share it with everyone for your reference. The details are as follows:
Here we use the recursive algorithm in JavaScript to calculate the factorial. This is a very common small example when you first learn programming. Compare it with the calculation method in JS.
The operation effect is as follows:
The specific code is as follows:
<html> <head> <meta http-equiv="content-type" content="text/html; charset=GB2312" /> <title>递归算法计算阶乘</title> </head> <body style="overflow:auto; padding:0px; margin:0px;"> <div style="font-size:14px; font-weight:bold; color:white; font-family:Arial, 宋体; background-color:#6090DA; padding:4px 10px;"> <script> function calc(n){ if(n>0)return(calc(n-1)*n); return(1); } document.write("正整数8的阶乘是"+calc(8)); document.write("<br>正整数16的阶乘是"+calc(16)); </script> </div> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.