2

I am trying to create a game for fun in Captivate. The game will mimic a wheel of fortune or spin the wheel format.

I am trying to put a different spin on it, and not have any repeating numbers.

Here is what I have so far:

 function getRandomInt (min, max) {
var jsRandomNumber = Math.floor (Math.random () * (max - min + 1)) + min;

    if(jsRandomNumber==1){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 1);
    }else if (jsRandomNumber==2){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 2);
    }else if (jsRandomNumber==3){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 3);
    }else if (jsRandomNumber==4){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 4);
    }else if (jsRandomNumber==5){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 5);
    }else if (jsRandomNumber==6){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 6);
    }else if (jsRandomNumber==7){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 7);
    }else if (jsRandomNumber==8){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 8);
    }


}

getRandomInt(1,8);

I don't understand how to prevent repetitive numbers. I also can assign values in Captivate if a slide has been viewed, however I don't understand how to skip that slide if the random number is generated. Any help is greatly appreciated.

  • What's wrong with simple `window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', jsRandomNumber)`? Also, it's not a good idea in general to modify something in `getter` methods. – raina77ow Sep 22 '15 at 18:51
  • I attempted that, it didn't work. I am sorry, I am very new to JS. So this whole thing is kind of over my head. – Undercover Batman Sep 22 '15 at 19:57

1 Answers1

0

You can have an array store what is pending and keep on deleting what has been shown.

function getRandomInt () {
  var min = 0;             //1st arr element index
  var max = arr.length-1;  // last arr element index

  if(max<0){
    return false;          //else repopulate the array or based on what you want
  }
  var randomIndex = Math.floor (Math.random () * (max - min + 1)) + min;
  var randomNumber = arr.splice(randomIndex,1)[0];
  //extract the item in that randomIndex position, 
  //since the array is global it will affect the array next time, 
  //we will not have that value in the next call.

  //window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', randomNumber);
  console.log(randomNumber);
}

//generate a random array to keep track
var arr = function(){
  var min = 1, max = 8, atr = [];
  for(var i=min; i<=max; i++){
    atr.push(i);
  }
  return atr;
}();


//run a few times to test.
getRandomInt();
getRandomInt();
getRandomInt();
getRandomInt();
getRandomInt();
joyBlanks
  • 6,419
  • 1
  • 22
  • 47
  • Thank you for the suggestion. When I input the JS and then run, it doesn't work. :( . I am very much a beginner so I am unsure why it is failing. – Undercover Batman Sep 22 '15 at 19:55
  • I actually commented the line `window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', randomNumber);` since I dont have rest of the code with me. You can go ahead and uncomment it and you will have the desired behaviour – joyBlanks Sep 22 '15 at 19:57
  • It now does the execute the random number, but it is repeating after multiple clicks. – Undercover Batman Sep 22 '15 at 20:06
  • can you post your whole code or make a [JSfiddle](http://jsfiddle.net) so I can check – joyBlanks Sep 22 '15 at 20:37