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

JavaScript uses recursive algorithm to calculate factorial example_javascript skills

WBOY
Release: 2016-05-16 15:47:17
Original
1575 people have browsed it

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

I hope this article will be helpful to everyone’s JavaScript programming design.

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!