Trivia Game - Using Variables and JavaScript to remove answered questions from the deck.

Create a trivia game that would:

  1. Randomly select a category from a list of 5, but limit the selection to only categories containing unanswered questions. 
  2. Display a random question in the selected category from a list of unanswered questions.
  3. End the game when all questions in all categories have been answered.

In other words, once you answer a question correctly, you won't see it again; answer it wrong, and it will keep randomly appearing until you get it right. Once you answer all the questions in a category, it will also be "removed" and you won't see it again.

The key was to use variables to track things and JavaScript Arrays. Many of you have already seen the JavaScript to generate a random number. I just took it a step further by defining an array based on variables, filtering the array to remove 0's, and then pulling a random number from the remaining value set.

The script looks something like the following:

//var player=GetPlayer();

//var nv1=player.GetVar("V1");
//var nv2=player.GetVar("V2");
//var nv3=player.GetVar("V3");
//var nv4=player.GetVar("V4");
//var nv5=player.GetVar("V5");

//var values1 = [nv1, nv2, nv3, nv4, nv5];
//var filtered1 = values1.filter(function(x) {return x>0;});

//var target = filtered1[Math.floor(Math.random() * filtered1.length)];

I don't have access to a server to post the published version, but below is the .story file. If someone wants to publish it and share a link, that would be awesome. Enjoy!

Update. Dave posted the published version for me HERE.

26 Replies