Home >Web Front-end >JS Tutorial >JavaScript uses recursive algorithm to calculate factorial example

JavaScript uses recursive algorithm to calculate factorial example

高洛峰
高洛峰Original
2017-02-08 15:27:521685browse

This article mainly introduces the use of recursive algorithms in JavaScript to calculate factorials. It briefly analyzes the related usage techniques of JavaScript recursive algorithms. It has certain reference value. Friends in need can refer to it.

The examples in this article describe JavaScript A method of calculating factorial using a recursive algorithm. Share it with everyone for your reference. The details are as follows:

Here we use the recursive algorithm in JavaScript to calculate the factorial. When you first learn programming, this is a very common small example. Compare it with the calculation method in JS.

The running effect is as follows:

JavaScript uses recursive algorithm to calculate factorial example

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;">
<p 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>
</p>
</body>
</html>

More JavaScript uses recursive algorithm to calculate factorial For examples related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Recursive function in JSNext article:Recursive function in JS