[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 138
  • Last Modified:

Avoiding the repetion of random numbers. HOW DO I PLACE MY CODE IN LOOP OF A FUNCTION

stiil struggling:
words=["Which is the capitol of Italy?","Which is the capitol of Spain?","Which is the capitol of France?","Which is the capitol of Holland?"];
randvalue=Math.round(Math.random()*4);
word=words[randvalue];
trace(word);

This was my help to the first question:
After declaring your array:

randValue = Math.round(Math.random()*words.length);
word = words.splice[randvalue, 1];  //removes that value from the array ensuring it's not repeated, and assigns it to word
trace word;

HOW DO I  place thhis in a function of a loop, that should do it. (THIS IS MY QUESTION!!!!!)
0
andreafp
Asked:
andreafp
1 Solution
 
SavongCommented:
Sorry about the wording, should have been "place in a function OR a loop", so for a loop:

words=["Which is the capitol of Italy?","Which is the capitol of Spain?","Which is the capitol of France?","Which is the capitol of Holland?"];
for(var i = 0; i < words.length; i++){
     randValue = Math.round(Math.random()*words.length);
     word = words.splice[randvalue, 1];  //removes that value from the array ensuring it's not repeated, and assigns it to word
     //do what you need to do here, assign to an array to store them randomly, or som other action
}

that will loop through, now keep in mind that that will loop through each of the items in a random order, but won't stop (as it's written) to do anything else.

or for a function to be called, place this on the first frame:
function getWord():String{
     randValue = Math.round(Math.random()*words.length);
     word = words.splice[randvalue, 1];  //removes that value from the array ensuring it's not repeated, and assigns it to word
     return word;
}

then if you call that function:
myNewWord = getWord();
It will get you a new word from your list.  This particular setup has no checking for whether you're at the end of the list though.  Feel free to respond here if any of that doesn't make sense.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now