如何在函数中使用另一个函数中的变量(组合计算器)



我是JS的新手,在使用另一个函数中的变量到我的新函数时遇到了困难。

目标是为组合(组合计算器(进行计算,但当我使用此脚本代码时,我不会得到输出。

var i, num, num2;
function factor() {
x = 1;
y = 1;
num = document.getElementById("num").value;
for (i = 1; i <= num; i++) {
x = x * i;
}
i = i - 1;
document.getElementById("res").innerHTML = "The factorial of the number " + i + " is: " + x;
num2 = document.getElementById("num2").value;
for (i = 1; i <= num2; i++) {
y = y * i;
}
i = i - 1;
document.getElementById("res2").innerHTML = "The factorial of the number " + i + " is: " + y;
}
function partTwo() {
a = y.factor(num - num2);
b = x.factor / a;
document.getElementById("res3").innerHTML = "Answer: ";
}
<!--Input-->
<h2 id="one"> Please enter first value for combination calculation </h2>
<input type="number" placeholder="Number 1" id="num"> <br> <br>
<!--Input 2-->
<h2 id="two"> Please enter second value for combination calculation </h2>
<input type="number" placeholder="Number 2" id="num2"> <br> <br>
<!--button-->
<button onclick="factor()"> ENTER </button>
<h2 id="res"> </h2>
<h2 id="res2"> </h2>
<button onclick="partTwo()"> CALCULATE </button>
<h2 id="res3"> </h2>

我本应该得到一个组合计算器的输出,需要帮助如何完成第二个函数

您不能使用超出其范围的变量,或者如果您想使用此变量传递参数试试这个:

var i, num, num2;
function factor() {
x = 1;
y = 1;
num = document.getElementById("num").value;
for(i = 1; i <= num; i++)  
{
x = x * i;
}
i = i - 1;  
document.getElementById("res").innerHTML = "The factorial of the number " + i + " is: " + x ;
num2 = document.getElementById("num2").value;
for(i = 1; i <= num2; i++)  
{
y = y * i;
}
i = i - 1;  
document.getElementById("res2").innerHTML = "The factorial of the number " + i + " is: " + y ;
partTwo(x,y)

}
function partTwo (x,y) {

a = y.factor(num - num2);
b = x.factor/a;
document.getElementById("res3").innerHTML = "Answer: " ;
}

如果要使函数成为全局变量,则可以从一个函数到另一个函数使用变量。

但根据你的partTwo()函数的格式,我相信你想在这里学习的是";类";在JavaScript中。

我重新构建了您的代码,并更改了HTML和JavaScript中的一些内容。

<h2 id="one"> Please enter first value for combination calculation </h2> 
<input type="number" placeholder="Number 1" id="num1"> <br> <br>
<!--Input 2-->
<h2 id="two"> Please enter second value for combination calculation </h2>
<input type="number" placeholder="Number 2" id="num2"> <br> <br>
<!--These buttons were replaced by ID so that I can add an event listener in the JS instead of onclick tag-->
<button id="enter"> ENTER </button>  
<h2 id="res"> </h2>
<h2 id="res2"> </h2>
<button id="calculate"> CALCULATE </button>
<h2 id="res3"> </h2>

以下是使用class而不是function:的JS代码

class myfactor {
constructor(num1, num2) {
this.num1 = num1;
this.num2 = num2;
}
x() {
var xval = 1;
for(var i = 1; i <= this.num1; i++)  
{
xval = xval * i;
}
i = i - 1;
return xval;
}

y() {
var yval = 1;
for(var i = 1; i <= this.num2; i++){
yval = yval * i;
}
i = i - 1;
return yval;
}
}

下面是代码的其余部分,您可以在其中找到";点击";两者的事件";输入";以及";计算";按钮:

var factor;
var num1;
var num2;
document.getElementById("enter").addEventListener("click", function(){
num1 = document.getElementById("num1").value;
num2 = document.getElementById("num2").value;

factor = new myfactor(num1, num2);

document.getElementById("res").innerHTML = "The factorial of the number " + num1 + " is: " + factor.x();
document.getElementById("res2").innerHTML = "The factorial of the number " + num2 + " is: " + factor.y();
});
document.getElementById("calculate").addEventListener("click", function(){
var a = factor.y() * (num1 - num2);
var b = factor.x() / a;
document.getElementById("res3").innerHTML = "Answer: " + b;
});

您的问题中不清楚您试图在这里输出什么,但我只是使用您的变量b作为最终输出,因为它在代码中有最后一个公式。

最新更新