Forum Discussion
Trivia Game - Using Variables and JavaScript to remove answered questions from the deck.
Ok, I think I get where you are going with this and a sequential loop should work for you. A sequential loop is basically a command statement that tells the computer to repeat the same sequence of commands a certain number of times.
The basic construct looks like this
for ("variable to track iterations with a starting value"; "condition test to stop looping"; "command to update the tracking variable at the end of each loop") {
"code you want to execute each loop";
}
That's it.
So here is a quick example you can try right now in this window. Open up your browser's console. (For most browsers, click in the address bar and push the F12 key.) Copy and paste the following code into the console and execute it.
for (var i = 0; i < 10; i += 1) {
console.log(i);
};
When you execute this code, you should see the numbers 0 through 9 returned. So here is what happened. We created a loop and declared a variable "i" with a value of 0. We created a stop condition to exit the loop as soon as the variable "i" was equal to 10. We incremented the value of the variable "i" by 1 each time we went through the loop. The code we executed during each loop was to write the current value of the variable "i" to the console log. Make sense?
If I apply this basic looping construct to your code, it would look like this if I wanted to loop through 50 times:
//only need to get the player once
var player=GetPlayer();
//start the loop
for (var i = 0; i < 50; i += 1) {
//add your code and close the statement with "}"
var nv1=player.GetVar("F1");
var nv2=player.GetVar("F2");
//to
var nv100=player.GetVar("F100");
var myArray = [nv1, nv2, ....., nv100];
var filtered1 = myArray.filter(function(x) {return x>0;});
var myNumber = filtered1[Math.floor(Math.random() * filtered1.length)];
player.SetVar("FM",myNumber);
var myVariable = "F" + myNumber;
player.SetVar(myVariable, "0");
};
Let me know how it goes and if this explanation made sense.
~ Owen