Forum Discussion
Trivia Game - Using Variables and JavaScript to remove answered questions from the deck.
Concept
Create a trivia game that would:
- Randomly select a category from a list of 5, but limit the selection to only categories containing unanswered questions.
- Display a random question in the selected category from a list of unanswered questions.
- 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.
How
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)];
//player.SetVar("ShowCategory",target);
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.
This is very cool use of JavaScript, Owen. I'll get this added to our downloads hub.
Would you mind sharing your example to our screencast and e-learning challenges?
Screencast challenge:
Elearning challenge:
Here's a link to the published example: http://articulate-heroes-authoring.s3.amazonaws.com/Examples/Storyline/SL2/TriviaGameArray/publish/story.html
- JeffreyRileyCommunity Member
David, I loved the example but it appear so small that I almost cannot read the questions and answers. Can you publish and enlarge the result? Thanks!
- OwenHoltSuper Hero
That is my fault as I designed it the way I would for the users I support. Sorry about that. I did, however, provide the source file in my original post. You should be able to DL the source file, make the size adjustments you need and republish it for yourself. :-)
- OwenHoltSuper Hero
Thanks Dave! I've posted it in the Elearning challenge and will screencast it soon, I promise.
- JamesBonney-EDECommunity Member
Really good Owen, nice and clear!
- VeronicaBudnikaSuper Hero
This is genius Owen, thanks so much for sharing!
- JewellMeyer-cbeCommunity Member
HI Owen!
Thank you so much for sharing this brilliant activity!
I was wondering if there was a way to get Graded Results once the learner is finished. Also, is there a way to stay in one scene/category, answer all of the questions and then randomly go to another scene/category?
Thank you, again!
~Kaylee
- OwenHoltSuper Hero
Yes and Yes. I think we are connected on LinkedIn now; let's set up some time to discuss your specific use case.
- KristinAugustaCommunity Member
Hi Owen - I tried to use this code and modify it in order to randomly generate a unique number and then show the corresponding slide layer. I theoretically understand creating an array and removing numbers from the array that have been used by setting their value to zero. I just can't make it work. Can you help a gal out? I'm trying to make a "random accolade generator" for my team's Trainer Appreciation Week next week. I can get it to choose random - but not unique yet.
Thank you!
- OwenHoltSuper Hero
Sorry for the late response, I've been at a leadership offsite. Any chance you can share your file so I can see what is going on under the hood?
I'm not sure I did a great job of explaining all of the details when I made this post, but there are some critical steps here.To keep it simple, let's assume that in Story line I have 5 variables to store values from 1 to 5 and I want to choose a random number. The array will do this fine, but when I send the value back to SL I also need a trigger to set the selected variables value to 0. If I fail to adjust the variables to 0 as they are selected, they will never be filtered out of my array. In my file, you will see that I perform this on the correct feedback layer for each question when the user presses the button to go back to the main menu. Make sense?
- KristinAugustaCommunity Member
Hi Owen -
Thank you so much for your response. Again - in theory - crystal clear. I am just not able to make the actual leap to coding it. Here's my .story file. Thank you!
- OwenHoltSuper Hero
AH HA! I see the issue!
When I originally posted this, the community "engine" kept ignoring my JavaScript code so I made each line of code a "comment". In JavaScript, any command line that begins with a double forward slash (//) is ignored. Nothing is happening in your file because every line of JavaScript code is being treated as a comment and not a command. Remove all the double slashes // in the code and it should work.
- KristinAugustaCommunity Member
Ahhhhh! Thank you so much! I am embarrassed that I didn't catch that - because I do know that - but I was so focused on whether or not I was getting the array and variables right, I ignored what was right in front of me.
Thank you for your help - you are a kind and generous forum participant!
- EvaHorneCommunity Member
Hi folks! I am currently using a version of this code to choose more than one number from the array by repeating parts of the code the number of times that I want it to run. For example, I have 100 flowers and I want to randomly choose 5 of them, so I copy/paste the last three lines of the code five times. It works that way, but will become cludgy when I get to the slide where I need for it to choose 50 of the flowers. I know that there are loops in javascript, but have so far not been able to figure out how to get a loop to work. Do any of you know how to loop this code? Thanks!