Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript - generate 3 random numbers that add to 100 and add them to 3 variables

Community Beginner ,
Jun 21, 2018 Jun 21, 2018

Hi Guys,

I am creating an elearning module using Captivate and as part of that i have created a 'Who wants to be a millionaire' style set of lifelines.

I have 3 variables audience1,  audience2 and audience3

I'm looking for some Javascript that will generate 3 numbers that add up to 100 and and them to 3 separate variables. e.g - audience1 = 20, audience2 = 50, audience3 = 30.

Has anyone created this style of game before? or can anyone point me in the right direction?

Thanks

TOPICS
Advanced , Advanced actions , Quizzing and LMS
2.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 21, 2018 Jun 21, 2018

I am not a JS expert, if you want a JS only solution, wait for a JS expert. I always combine with Advanced or Shared actions

Did you read my explanation in this blog posts:

Playing with Numbers - part 1 - Captivate blog

Playing with Numbers - part 2 - Captivate blog

I would generate two random variables in the same way as described in those posts, and calculate the value of the third one with an Expression command in an advanced action. Too simple perhaps?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 21, 2018 Jun 21, 2018

That is extremely helpful, a JS only solution would be beneficial but I like leaning more about Captivate and this will certainly help with that.

Thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 21, 2018 Jun 21, 2018

Only a personal opinon, but I think too many CP-users switch to JS before having explored the capablities of advanced and shared actions (which are both converted to JS on runtime), more accessible to everyone. No need for syntax rules.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 21, 2018 Jun 21, 2018
LATEST

Here are 2 versions of javascript to generate the three numbers and pass them back to Captivate.

NOTE - this first simple version limits the first and second numbers to a range of 1 - 45 to ensure there is some room for a third number.

var first = Math.floor((Math.random() * 45) + 1);

var second = Math.floor((Math.random() * 45) + 1);

var third = 100 - (first + second);

window.cpAPIInterface.setVariableValue("audience1", first);

window.cpAPIInterface.setVariableValue("audience2", second);

window.cpAPIInterface.setVariableValue("audience3", third);

If you want to always have a minimum value for the numbers, change the statements accordingly. This example will generate a number from 10 - 45:

var first = Math.floor((Math.random() * 35) + 10);

---------------Second version with a loop----------

This version allows values for all three numbers in the range of 10 - 80, but ensures that the total of all three numbers remains 100.

var first,second,third;

var sum = 100;

while (sum > 90){

     first = Math.floor((Math.random() * 70) + 10);

     second = Math.floor((Math.random() * 70) + 10);

     sum = first + second;

    }

third = 100 - sum;

window.cpAPIInterface.setVariableValue("audience1", first);

window.cpAPIInterface.setVariableValue("audience2", second);

window.cpAPIInterface.setVariableValue("audience3", third);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources