jeudi 28 mai 2015

Sharing Javascript variables between function

So I have a few variables that I need to use within two separate functions. The first function essentially uses the variables to calculate and then display something (a conjugated verb). The second function uses the variables to see if the user's answers were correct, and changes some HTML styling accordingly.

However, these variables are calculated randomly, like this:

function randomIntFromInterval(min,max) {
return Math.floor(Math.random()*(max-min+1)+min); }

var tense = randomIntFromInterval(1, 6);
var person = randomIntFromInterval(1, 3);
var number = randomIntFromInterval(1, 2);
var voice = randomIntFromInterval(1, 2);

I can't declare them as global variables outside a function, because they need to be recalculated every time the first function is called. I can't declare them in their own function and call it inside the original two functions, because both of the functions needs the same values.

How can I do this?




Aucun commentaire:

Enregistrer un commentaire