Home > Web Front-end > JS Tutorial > body text

JavaScript uses recursive algorithm to calculate factorial example

高洛峰
Release: 2017-02-08 15:27:52
Original
1644 people have browsed it

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>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template