I'm writing a simple quiz program. The person has to answer a fixed number of questions.
I have a bank of answer/question pairs set up as a database.
The answer is stored before the question because each answer is a single word or number and I don't know how to access a dataset with a multi-word 'key'
how do extract an answer/question pair from a dataset at random?
Arrays appear to be accessed by words instead of numbers, 1st, 2nd.
It's hard to see what value this method of extraction has
Is there any way to assess an array by number, so I can use some math?
So far the only way I can think of is to use a subarray, e.g.
(set: $Fruit to (a: "banana", "apple", "pear", "peach"))
(set: $Key to 3)
(subarray: $Fruit, $Key, $Key)
(set: $Key to 2)
(subarray: $Fruit, $Key, $Key)
Based on the above, one answer to your first question is to use macros to generate a key like so. ... or if the list of questions were stored in an array you could shuffled them like so:
One way would be to use the (datanames:) macro to get a list of all the answers and then use the (either:) macro to randomly pick an answer.
2: You can access array elements using numbers does the same as
So this will work
The actual layout is a bit clunky, but largely working
Still using the subarray to extract individual elements of an array as I can't find another method.
The first passage (Quiz) does the setup and asks three questions.
This part is working.
But the passage for checking the results (Check the answers)
gives me an "Incorrect" on each attempt, even though they appear to match
eg. your customScripts.submitName function.
b. The (datamap:) macro documentation states that the names(keys) should be Strings, so they should not be other data types like numbers even if they appear to work.
You can use the string version of a number, so your 6 should be "6", 4 should be "7" and 4 should be "4"
c. HTML text input elements contain String (text) values, so any when comparing your $Answer1, $Answer2, $Answer3 variables to another variable that other variable also need to contain a String value for the comparison to be true.
This was one of the issues causing your Check the answers passage to sometimes fail.
eg. If the reader answered 4 to the first question then $Answer1 will contain "4", so the value within $A1 needs to be a String value of "4" (not a number 4) for the two variables to be equal.
d. Accessing an array element or a datamap key using an expression. I provided examples of how to use an expression to access both of these in my previous comment, they included both using a literal and a variable. The documentation states that the expression should be within parenthesis. eg. (expression) e. You can use a startup tagged passage to initialize (give default values to) your story's variables, this is a good idea especially for variables that don't change their values like your $Quest.
The following are corrected versions of your examples, I have add new $Encouragements variable and formatted the code a little.
1. Quiz passage 2. Check the answers passage
warning: There are a number of other potential issues with the code.
1. You are assuming that the reader will enter their answers using only lowercase letters, so if they answer "FORK" or "Fork" (etc) to the question requiring "fork" they will be told they answered incorrectly when they didn't.
2. You are also assuming that the reader will answer "4" instead of "four" to the questions requiring a numerical answer, which they may if they have previously answered questions requiring words for answers.
The solution for this either requires you to: tell the readers to enter numerical answers using numbers, for you to check for both "4" and "four", or for you to expect people to complain that the story tells them they are incorrect when they are not.
I really appreciate your help
Sorry about not including the java code. I have found several examples using it in the forum and thought it was “well known”.
In my original code I had allowed for lower case and cap case, but had taken it out to simplify debugging.
This module did not allow for spelt numbers, because it’s for two little kids aged 5 and 3, so they probably wouldn’t spell them. But I fully agree, it should be catered for. I know VBA (and used to program in many languages many years ago – even before ‘object oriented code’ existed), so I expect java code to be relatively easy to pick up.
This is part of something bigger.
I have been writing for years and recently I have been writing fairy tales for a friend’s kids (and stories for my friends). So CYOA seemed like a fun way to progress.
A bit of research led me to Twine.
Again, it was meant to be a simple Choose your own adventure. But once I got enTwined, I decided to broaden the scope.
The story begins in a 12 room + 2 gardens + courtyard + blacksmith shop, palace.
The player has to explore the palace to find a number of items (usual stuff), and must pick up a sidekick, and meet people in a certain order to get permission to go outside, then get the key, and only then can they 'pick up' the carriage, pony, picnic basket etc.
The palace section is finished and working.
When the player leaves the palace they enter the kingdom (5 x 4 grid), with an adventure/challenge in each. Some challenges can be overcome only if a certain item has been brought form the palace, others can be overcome without the item, but it’s harder.
But if the have forgotten the carriage or pony they can't go anywhere, and if they have forgotten the map or food they can move only one square E, N or W before returning.
In one grid square a bridge needs to be crossed, and the troll who guards it asks three questions to allow passage – and this is what the question module is needed for.
Others require a word puzzle to be solved, and so on. And most squares can be entered more than once and from different directions.
And sometimes the player has to return to the palace. E.g. when the pony loses a shoe it has to be taken back to the blacksmith.
I have also written a few short fairy tales, which can be read from a link that appears as a reward for accomplishing something.
It was useless without graphics, and I didn’t want the bother of being online and slow image hosting, so I have embedded them in base64 (currently over 70 images, with more to come) .
There is 'treasure' to be found only if certain tasks have been completed, but I'm still working that one out.
I have about ¼ of the kingdom finished and working.
So while it’s meant to be for a couple of kids, I’m having as much fun writing it.
I personally use a slightly different version of that function called customScripts.updateNamedHook which supports both text inputs and text area elements, and I think it's name more clearly describes what the function does.