DEV Community

BadRockk
BadRockk

Posted on

dynamic loop for parameter combinations

Hi there,

I'm doing JavaScript programming for a short time and I could solve most of my problems on my own searching the web. Most of my programming has been done in Excel VBA so my thinking maybe is still influenced by that.

I use JavaScript in a calculation tool to performe multiple calculations automatically.

My problem is that I need to setup a dynamic loop to start the calculation with all possible combinations of parameter values.

I have multiple objects like:

parameter_01 = {
  trigger: "trigger_01"
  values : [0,2,5]
  }
parameter_02 = {
  trigger: "trigger_02"
  values : [3,6]
  }
...
parameter_xx = {
  trigger: "trigger_xx"
  values : [8,5,9,7,5]
  }
Enter fullscreen mode Exit fullscreen mode

The number of parameters and values can change so I don't want to implement a fixed serial of loops iterating each parameter. My first thought is to declare an additional object that contains all parameters I want to take into the calculation:

all_parameters = [parameter_01, parameter_02, ... , parameter_xx]
Enter fullscreen mode Exit fullscreen mode

Then I would need someting like this:

for (var each_paramter of all_parameters) {
    for (var i of each_parameter[values]) {
        setAttr(each_parameter[trigger],i); // This is to set the parameter value in my calculation program
        for (var each_paramter+1 of all_parameters) {
             for (var j of each_parameter+1[values]) {
             setAttr(each_parameter+1[trigger],j);
             ...
                 for (var each_paramter+x of all_parameters) {
                      for (var x of each_parameter+x[values]) {
                      setAttr(each_parameter+1[trigger],x);
                      run_calulation;
                      }
                 }
             }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Is it possible to create such a dynamic loop? Maybe I'm just searching with the wrong key words. I would appreciate every tip or link that could help me to solve this problem.

Top comments (1)

Collapse
 
72bizsolutions profile image
Clemence Hoe

I use Ring programming language to do dynamic for loop:

Here is example:

// Dynamic loop example

print "Start : " give nStart nStart = 0 + nStart
print "End : " give nEnd nEnd = 0 + nEnd
print "Step : " give nStep nStep = 0 + nStep

for x = nStart to nEnd step nStep
? x // ? is shortform for print(x + nl), nl = new line
next

Ring is easy, have a try!