How to create a function in javascript that creates some combination of arrays

How to create a function in javascript that creates some combination of arrays
0.0 0

#1

I’m trying to create a function that will create some combinations .the combinations of arrays i want to create are stored at the “ev0s” variable …so far i know that my mistake is really basic,…at the for loops ,.ive been always having a hard time trying to understand it…any help is really welcome and also the explanations, thanks community.
here is my code so far:

//Declaring variables values
var apers = [“f/1”, “f/1.4”, “f/2”, “f/2.8”, “f/4”, “f/5.6”, “f/8”, “f/11”, “f/16”,
“f/22”, “f/32”, “f/44”, “f/64”, “f/88”];

var shrs = [“128m”, “64m”, “32m”, “16m”, “8m”, “4m”, “2m”, “60s”, “30s”, “15s”, “8s”,
“4s”, “2s”, “1s”, “1/2s”,“1/4th”, “1/8th”, “1/15th”, “1/30th”, “1/60th”,
“1/125th”, “1/500th”, “1/1000th”, “1/2000th”, “1/4000th”, “1/8000th”];

var isos = [“50”, “100”, “200”,“400”, “800”, “1600”, “3200”, “6400”, “12800”, “25600”, “51200”,
“102400”, “204800”, “409600”, “819200”];

var evs = ["-15", “-14”, “-13”, “-12”, “-11”, “-10”, “-9”, “-8”, “-7”, “-6”, “-5”, “-4”,
"-3", “-2”, “-1”, “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “11”,
“12”, “13”, “14”, “15”, “16”, “17”, “18”, “19”, “20”, “21”, “22”, “23”, “24”,
“25”, “26”, “27”, “28”, “29”, “30”, “31”, “32”, “33”, “34”,“35”, “36”, “37”,
“38”];

//Combinations of Exposure Value 0
var ev0s = [[apers[0] + shrs[13] + isos[1]], [apers[1] + shrs[12] + isos[1]],
[apers[2] + shrs[11] + isos[1]], [apers[3] + shrs[10] + isos[1]],
[apers[4] + shrs[9] + isos[1]], [apers[5] + shrs[8] + isos[1]],
[apers[6] + shrs[7] + isos[1]], [apers[7] + shrs[6] + isos[1]],
[apers[8] + shrs[5] + isos[1]], [apers[9] + shrs[4] + isos[1]],
[apers[10] + shrs[3] + isos[1]], [apers[11] + shrs[2] + isos[1]],
[apers[12] + shrs[1] + isos[1]], [apers[13] + shrs[0] + isos[1]]];

//instead of writing all of the combinations of exposure value 0, the intention is 
// create them with some function

function eVConst(arr){

 var ev0 = [];
 
 for(var a = 0; a < arr.length;  a++){
     for(var s = 0; s < arr.length; s--){
        for(var i = 0; i < arr.length;  ){
           while(i === isos[1]){
            ev0 += arr[a][s][i];
           }
        }
     }
 }
return ev0;

}

eVConst([apers[0], shrs[13], isos[1]]);


#2

The second for loop sets s = 0, but decrements s with the condition to run while s is less than the length of arr. Did you mean to do s++? Otherwise, this is an infinite loop. The 3rd for loop doesn’t do anything to i. Also, when you’re calling eVConst, you’re calling it with 3 parameters. The function definition only specifies one parameter.


#3

yeah i notice that.that I’ve create a infinite loop…well the idea is apers[0] start there at index 0 of the array(variable) apers…and increments to index 1,…but at the same time shrs[13],…starts at the index 13 of the variable shrs,…and go backwards to index 0


#4

will your 3 arrays always have same length? Also you are only adding isos[1] in your example array do you want it like this or is it just typo?

in case your arrays length is always equal here is a function:

function eVConst(arrApers, arrShrs, arrIsos){
 var myArr = [];
 var length = 0;
 if(arrApers.length >= arrShrs.length) {
  length = arrApers.length;
}
else {
  length = arrShrs.length;
 
}
  for( i = 0; i < length; i++) {
    var tempArr = [];
    var shrsIndex = arrShrs.length -1 - i ;
   if(arrApers[i] !== undefined) {
    tempArr.push(arrApers[i]);
   }
   if(shrsIndex >= 0)  {
    tempArr.push(arrShrs[shrsIndex]);
    }
    tempArr.push(arrIsos[1]);
   myArr.push(tempArr);
}
return myArr;
}

Edited my answer, I think it should work, but its not tested and might contain some syntax error as well.

https://codepen.io/gmdivani/pen/JJapRL?editors=0010


#5

no they won’t…the 3 arrays don’t have the same length,…and yes the function is only adding isos[1],sorry the function involves the 3 arrays,where isos[1] are constants ,don’t change. only the values of var apers & var shrs change…and i could possible change the order of the array shrs to avoid an infinite loop.


#6

Ok I think after several edits my function should work, please try it and let me know if it helps.


#7

im testing the function ,…its not showing errors ,…but also I’m getting nothing/no result.


#8

I’m going out on a limb here and wondering if we need a function. If your goal is to create the ev0s array, could a simple map work?

var ev0s = apers.map(function(value, index, arr){
  return [value, shrs[arr.length-index-1], isos[1]]
});

#9

I tested it and it works fine, do you have JQuery referenced? that might be the reason. also I think DaveC just offered a simpler version.


#10

yeah you’re right a “simple map work” will do the work !


#11

:blush: Glad it worked.


#12

a question if i would like to try other combinations of the ev0s…per example lets say i would try to create ev1s instead:
to get an out put like this:

//combinations of Exposure Values 1
var ev1s = [[apers[0] + shrs[14] + isos[1]], [apers[1] + shrs[13] + isos[1]],
[apers[2] + shrs[12] + isos[1]], [apers[3] + shrs[11] + isos[1]],
[apers[4] + shrs[10] + isos[1]], [apers[5] + shrs[9] + isos[1]],
[apers[6] + shrs[8] + isos[1]], [apers[7] + shrs[7] + isos[1]],
[apers[8] + shrs[6] + isos[1]], [apers[9] + shrs[5] + isos[1]],
[apers[10] + shrs[4] + isos[1]], [apers[11] + shrs[3] + isos[1]],
[apers[12] + shrs[2] + isos[1]], [apers[13] + shrs[1] + isos[1]]];


#13

For that, you should put it back into a function and send an additional param. The math on that shrs item starts to look a little crazy, but I thing this works.

function evConst(evLevel){
  return apers.map(function(value, index, arr){
    return [value, shrs[arr.length-index-1+evLevel], isos[1]]
  });
}

console.log(evConst(1));

(need to reply here as I’m “out of replies” for 24 hours. Guess I’m still too new :stuck_out_tongue_winking_eye: )

Yes, you could recreate the function above to map out all of your evs values. They’re numbers with indexes, so similar math applies. You’ll have some fun decisions on how that math should look. 54 items in your evs array doesn’t map cleanly to any of your other arrays, so you might have to cherry pick every second item or get creative another way.

Let us know how it goes. I’ll reply again if there are still questions when I have a proper reply count.


#14

well first thanks for your help and knowledge/time,…
heres the thing:
as you can see on my original post,at the variable evs i need to build from ev-15s to ev38s,…you already hep me to build ev0s…that means that the map work you already told me is to build evs[15](thats the ev0s variable),…as i say i need to build from ev-15s to ev38s,(from index 0 to index 53 of the evs array)…should i make a map work for each value of evs array or its possible code a function to create all of that combinations ?