Struggling to write this code.
I'm trying to calculate the highest value from a 2 objects. I started separate "S" and "P" objects:
var S = [
{ id: '1', value: '##' },
{ id: '2', value: '##' },
{ id: '3', value: '##' },
{ id: 'N', value: '##' }
];
var P = [
{ id: '1', value: '##' },
{ id: '2', value: '##' },
{ id: '3', value: '##' },
{ id: 'N', value: '##' }
];
I created an 3rd object:
var myobject = {
'S1' = {
'P1' = '25',
'P2' = '32',
'P3' = '65',
'PN' = '##'
},
'S2' = {
'P1' = '24',
'P2' = '31',
'P3' = '64',
'PN' = '##'
},
'S3' = {
'P1' = '26',
'P2' = '33',
'P3' = '66',
'PN' = '##'
},
'SN' = {
'P1' = '##',
'P2' = '##',
'P3' = '##',
'PN' = '##'
}
};
And I need to iterate through all the values to see with combination produces the highest value, for example:
S1.P1 + S2.P1 + S3.P1 = ?
S1.P1 + S2.P1 + S3.P2 = ?
S1.P1 + S2.P1 + S3.P3 = ?
S1.P1 + S2.P2 + S3.P1 = ?
S1.P1 + S2.P2 + S3.P2 = ?
S1.P1 + S2.P2 + S3.P3 = ?
...
The answer I'm looking for, using the example values above, is:
S1.P3 + S2.P3 + S3.P3 = 195
To complicate things, in some cases, a "P" value may only be used once in the equation:
var P = [
{ id: '1', value: '##' },
{ id: '2', value: '##' },
{ id: '3', value: '##', once: true },
{ id: 'N', value: '##' }
];
If "P3" could only be used once, the answer I'm looking for, using the example values above, is:
S1.P2 + S2.P2 + S3.P3 = 129;
I'm guessing the requires a little recursion....but my head hurts.
EDIT
I'm getting lost in the loops trying to create the calculation, for example:
foreach "S"
foreach "P"
foreach "S"
foreach "P"
....
Suggestions?