Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Script to generate random questions and answers

Avatar

Level 1

I would like to know how and what is a better way to the following scenario:

I have 8 text fields in the form.

4 fields for questions and 4 fields for answers.

By clicking on a button I would like to generate questions and answers in the 8 fields. Like a random selection of questions and answers.

I'm a beginner with LiveCycle.

Could someone help me with this question?

Thank you.

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi,

Sounds like quite a task to undertake.

First you would need to have the questions and answers stored in the form, probably in hidden fields. I would suggest a table, first column the question, second column the answer.

What i would do is create a random number generator, it needs a button for the script and a numeric field (which will be hidden)

form1.#subform[0].Button1::click - (JavaScript, client)

//random number between 1 and 26
var myRN = Math.random(); //create a variable for the random code
var lower = 1; //this is the lowest 'question answer table row number'
var upper = 26; //this is the highest ''question answer table row number'

NumericField1.rawValue = Math.floor(myRN*(upper - lower)) + lower; // show random number in numeric field

After generating the number you will then need to use logic to show the question and answer based on the random number. Something like:

if(NumericField1.rawValue == "3") //if the random number is 3, execute the code

{

//show the question and answer in row 3 in the textfields that are visible to the user.

question1TextField.rawValue = qaTable.Row3.question.rawValue;

answer1TextField.rawValue = qaTable.Row3.answer.rawValue;

}

In my example:

NumericField1 is the random number object

question1TextField is the first visible text field to show the question

answer1TextField is the first visible answer field to show the answer

qaTable is the name of the hidden table storing the questions and answers

Row3.question - question is a textField on Row3 of the table holding the question

Row3.answer - answer is a textField on Row3 of the table holding the answer

Here is a start for you, there are many considerations to look at.

• If you are wanting to show 4 questions and answers with one click, it is possible that your random number might show the same number and you will have the questions and answers repeated.

• To show 4 items at once, you probably need 4 random numbers generated.

• You will need to add more logic to test if a textfield is empty or not. Meaning, in my code, i just sent the question and answer to the first visible text fields, you need to add logic so that if the first textfield is not empty, it will place the question and answer in the next text field...and so on. If you do use 4 random generators  and the first field is not empty, you could then use random generator 2 etc

I hope this helps to give you something to go on.

View solution in original post

6 Replies

Avatar

Level 1

I'm sorry.

But could someone help me with this question?

Avatar

Correct answer by
Level 7

Hi,

Sounds like quite a task to undertake.

First you would need to have the questions and answers stored in the form, probably in hidden fields. I would suggest a table, first column the question, second column the answer.

What i would do is create a random number generator, it needs a button for the script and a numeric field (which will be hidden)

form1.#subform[0].Button1::click - (JavaScript, client)

//random number between 1 and 26
var myRN = Math.random(); //create a variable for the random code
var lower = 1; //this is the lowest 'question answer table row number'
var upper = 26; //this is the highest ''question answer table row number'

NumericField1.rawValue = Math.floor(myRN*(upper - lower)) + lower; // show random number in numeric field

After generating the number you will then need to use logic to show the question and answer based on the random number. Something like:

if(NumericField1.rawValue == "3") //if the random number is 3, execute the code

{

//show the question and answer in row 3 in the textfields that are visible to the user.

question1TextField.rawValue = qaTable.Row3.question.rawValue;

answer1TextField.rawValue = qaTable.Row3.answer.rawValue;

}

In my example:

NumericField1 is the random number object

question1TextField is the first visible text field to show the question

answer1TextField is the first visible answer field to show the answer

qaTable is the name of the hidden table storing the questions and answers

Row3.question - question is a textField on Row3 of the table holding the question

Row3.answer - answer is a textField on Row3 of the table holding the answer

Here is a start for you, there are many considerations to look at.

• If you are wanting to show 4 questions and answers with one click, it is possible that your random number might show the same number and you will have the questions and answers repeated.

• To show 4 items at once, you probably need 4 random numbers generated.

• You will need to add more logic to test if a textfield is empty or not. Meaning, in my code, i just sent the question and answer to the first visible text fields, you need to add logic so that if the first textfield is not empty, it will place the question and answer in the next text field...and so on. If you do use 4 random generators  and the first field is not empty, you could then use random generator 2 etc

I hope this helps to give you something to go on.

Avatar

Level 1

Hi MinusZero

I really really appreciate your answer.

I'm gonna try it.

Thank you very much.

Avatar

Level 1

Hi MinusZero.

I tried your code.

The simple solution was created 2 buttons for generating the questions and answers.

Like you sad "its possible created 4 questions and answers by one click", but I need more time. It's not necessary at this moment.

Two buttons figure out all the needs.

Once again.

Thank you very much for your code and share your knowledge.

I really really appreciate it.

If you wanna see the form: https://drive.google.com/open?id=1nZDqarHLviTO4jERntPJ-YbiTvZSsi-F

Now I’m gonna make some adjustments

Avatar

Level 7

No problem at all. I am glad to help out.

I'll check out your form later.