/ test 10 times on numbers 1 and 2
for (j=0; j< 10; j++){
operation = operators[ random(operators.length) ];
trace( operation(1, 2) );
}
for (_ ; _; _ ){…} is a method of looping through the same code many times over again. It uses numbers to count how many times to loop through the code (The code between the { and }, in this case
operation = operators[ random(operators.length) ];
trace( operation(1, 2) );
is what is being looped through). The first part of the for loop indicates the starting value of the number (j) controlling the loops. Here I started it at 0. The next part, after the semicolon, tells the for loop whether or not it should continue to loop. He I used j< 10 meaning, as long as the value in j is less than 10, keep repeating that code over and over again. The part after that is where the number is changed to determine when the loop stops. Here 1 is added to j every loop (using the ++ operator). So after the first loop, j is no longer 0 but 1. The following loop will make j 2, then 3 and 4 and so on… so on until j is no longer less than 10 at which point the loop stops.
operation = operators[ random(operators.length) ];
assigns a variable operation to something in the operators array. Each element of the operators array is a function, so operation itself will become a function. What element of the operators array it becomes is determined by
random(operators.length)
This choses a random number from 0 upto but not including the length of the array. Because the operators array length is 4, this returns a number from 0-3, which make up the 4 indexes of the array.
operators[0] == Math.sum
operators[1] == Math.subtract
operators[2] == Math.multiply
operators[3] == Math.divide
so operation will be one of those functions randomly. Following that is the actual function call of operation (since its assigned to be a function, it must be used as it is a function)
trace( operation(1, 2) );
This not only calls the function (with the arguments 1 and 2) but it traces the result in the output window. Because operation is a random operation of either sum, subtract, multiply or divide, the outcome of the trace will be either 1 + 2, 1 - 2, 1 * 2, or 1 / 2 since operation does any one of those things depending on what it was randomly assigned to from the previous line.
Because those two lines are within the { and } of the for loop, they are run through 10 times and the trace displays 10 different randomly selected operations of sum, subtract, multiply or divide performed on the numbers 1 and 2
