javascript计算1/1!+1/2!+1/3!+……+1/50!
时间: 2023-12-07 12:39:49 浏览: 122
javascript做的计算器
以下是JavaScript计算1/1!+1/2!+1/3!+……+1/50!的代码:
```javascript
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
function sum() {
var result = 0;
for (var i = 1; i <= 50; i++) {
result += 1 / factorial(i);
}
return result;
}
console.log(sum()); // 输出:1.7182818284590455
```
阅读全文