【发布时间】:2020-02-08 02:35:05
【问题描述】:
我正在尝试运行一个递归函数来计算 HTML 输入中数字的阶乘。我已经知道如何通过迭代来做到这一点,我想递归地实现结果。但是当我这样做时,我得到一个“递归过多”的错误。我错过了什么?
HTML
<input type="text" name="factorial_input" placeholder="Enter a number">
<div class="button" onclick="calculateFactorial()">Let's see the factorial</div>
JS
function calculateFactorial(getInputValue) {
getInputValue = document.querySelector("input").value;
if (!getInputValue) {
console.log("You must enter a number");
} else if (getInputValue == 0) {
return console.log("1");
} else {
return console.log(getInputValue *= calculateFactorial(getInputValue - 1));
}
// console.log(getInputValue);
}
【问题讨论】:
标签: javascript html recursion input