taking variables from one function to use in another function
Question by Paddy Hallihan
i’m creating a game in javascript for college and i have a button to call a function but i need it to include variables from another function
if i put the function inside the other function where the variables are the button doesn’t call it
is there a way of doing this or to get the variables that were created in the first function and use them in the second function
it is a simple maths game where you get a random number a random sum and another random number
(i.e. 5+7, 4*3, 2/6, etc,)
so i’ve got a function to show the random number using
var ranNum1a=Math.floor(Math.random()*10);
i need the other function to check the users answer but i need to know what the random number was from the other function
Answer by Starx
For simplest case, passing variables to another function might be the best approach to this problem.
Here is an illustration:
fucntion f1(var1, var2) {
//do you stuff then pass the variable to another function
f2(var1, var2);
}
function f2(v1, v2) {
// here v1 will get the value from var1 and v2 from var2 if passed from within the function f1
}