I am new in JS and I'm having difficulty with using a variable from another function to my new function.
the goal is to compute for a combination (combination calculator) but when I use this script code I don't get an output. Please help
`
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: " ;
}
`
here is the html code
`
<!--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>
`
I was supposed to get an output for a combination calculator and need help how to do the second function
CodePudding user response:
You can't use the variables out of their scope or if you want to use this variable pass through the parameter try this:
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: " ;
}
CodePudding user response:
It is possible to use variables from one function to another if you're going to make it a global variable.
But based on the format of your partTwo()
function, I believe what you want to learn here is the use of "class" in JavaScript.
I reconstructed your code and changed a few stuff in your HTML and 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>
Here's the JS code using class
instead of function
:
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;
}
}
And here's the rest of the code, wherein you can find the "click" events for both "enter" and "calculate" button:
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;
});
It is unclear in your question what you are trying to output here, but I just used your variable b
as the final output since it has the last formula in the code.