博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js实现阶乘
阅读量:7246 次
发布时间:2019-06-29

本文共 467 字,大约阅读时间需要 1 分钟。

//while循环实现 function calNum(n) {    var product = 1;    while(n > 1){
//1*5*4*3*2,1*n*(n-1)*(n-2)*...*2 product *= n; n--; } return product;}console.log(calNum(5))

 

//js实现阶乘	function calNum(n){		var a = 1, str = '1*';		for (var i = 2; i <= n; i++) {			str += i + '*';			a *= i;		}		str = str.substr(0,str.length-1);		return str + '=' +a;	}	console.log(calNum(5));       //打印结果为:1*2*3*4*5=120

  

转载于:https://www.cnblogs.com/eyunhua/p/5775623.html

你可能感兴趣的文章
mySQL (关系型数据库管理系统)
查看>>
Centos7配置Apache实现HTTPS
查看>>
npm的使用
查看>>
2018.12.26|区块链技术头条
查看>>
SharePoint:使用Indexed Column提高SharePoint 大型文档库或列表访问
查看>>
java8的时间和`Date`的对比
查看>>
MyEclipse开发教程:REST Web Service(二)
查看>>
【更新】CLion v2018.3发布(四):单元测试和编译数据库验证
查看>>
员工离职原因,只有两点最真实,其他都是扯淡!
查看>>
在esx server VI里导入其它虚拟机
查看>>
Linux剩余空间显示不一致的问题
查看>>
网络学习(八)Windows Server 2003 SP2系统安装
查看>>
SVN 配置
查看>>
Linux通信命令
查看>>
监测和管理Xcache状态
查看>>
有关Linux邮件的基础知识
查看>>
shell编程中的小问题
查看>>
Compare Version Numbers leetcode
查看>>
我的友情链接
查看>>
配置WebLogic数据源
查看>>