I have a bit of a logic problem with my code here, and was wondering if anybody could point me in the right direction.
Constraints:
- Each item pushed will be an event, grouped by "name" and displayed inline in a chart.
- EPC, M1, or M2 may have the value of null.
- An event should be created with a 'start' of first non-null timeline and 'end' of the next non-null timeline value. (no need to check beyond m2 since none will contain null value). There should be at least 4 milestones in every group.
- Should continue until no more 'groups' exist (content[i].name).
Currently, my code has a couple of issues:
- Only creates one event per group.
- EPC not accounted for in iteration, and unhandled if start and non-null.
Here is the code:
getMileStone = function(obj) {
if(!obj) { return };
propCounter = 1;
newcount = 0;
for(propCounter = 1; propCounter <= 7; propCounter++) {
if(obj.timeline["m" + propCounter]) {
data[newcount] = {key: "m" + propCounter, value: obj.timeline["m" + propCounter]};
return data[newcount];
newcount++;
}
}
};
getSecondStone = function(obj) {
switch(getMileStone(current).key){
case 'm1': return {value: obj.timeline['m2']};
break;
case 'm2': return {value: obj.timeline['m3']};
break;
case 'm3': return {value: obj.timeline['m4']};
break;
case 'm4': return {value: obj.timeline['m5']};
break;
case 'm5': return {value: obj.timeline['m6']};
break;
case 'm6': return {value: obj.timeline['m7']};
break;
default: return {value: obj.timeline['m1']};
}
};
for(i=0;i< content.length;i++) {
current = content[i];
firstMileStone = getMileStone(current);
secondMileStone = getSecondStone(current);
result.push({
'start': new Date(current.epc || firstMileStone.value),
'end': new Date(current.m1 || secondMileStone.value),
'content': firstMileStone.key,
'group' : current.name,
'className' : firstMileStone.key
});
}
And here is the array content being read:
content = [{
"name": "5-HP-N/A-N/A-F8",
"node": {
"name": "5",
"id": 14
},
"timeline": {
"epc": null,
"m1": null,
"m2": null,
"m3": 1554087600000,
"m4": 1593572400000,
"m5": 1625108400000,
"m6": 1641006000000,
"m7": 1656644400000
},
"fab": {
"name": "F8",
"id": 1
}
},
{
"name": "7-HP-N/A-N/A-F8",
"node": {
"name": "7",
"id": 15
},
"timeline": {
"epc": null,
"m1": null,
"m2": null,
"m3": 1491015600000,
"m4": 1530414000000,
"m5": 1561950000000,
"m6": 1577847600000,
"m7": 1593572400000
},
"fab": {
"name": "F8",
"id": 1
}
},
{
"name": "5-XM-N/A-PLT-F8",
"node": {
"name": "5",
"id": 14
},
"timeline": {
"epc": null,
"m1": null,
"m2": null,
"m3": 1554087600000,
"m4": 1593572400000,
"m5": 1625108400000,
"m6": 1641006000000,
"m7": 1656644400000
},
"fab": {
"name": "F8",
"id": 1
}
},
{
"name": "40-LP-TFS-FSL-F7",
"node": {
"name": "40",
"id": 4
},
"timeline": {
"epc": 1349060400000,
"m1": null,
"m2": null,
"m3": 1262314800000,
"m4": 1301626800000,
"m5": 1333249200000,
"m6": 1341111600000,
"m7": 1357009200000
},
"fab": {
"name": "F7",
"id": 3
}
},
{
"name": "40-LP-SST-TI-F7",
"node": {
"name": "40",
"id": 4
},
"timeline": {
"epc": 1349060400000,
"m1": null,
"m2": null,
"m3": 1262314800000,
"m4": 1301626800000,
"m5": 1333249200000,
"m6": 1341111600000,
"m7": 1357009200000
},
"fab": {
"name": "F7",
"id": 3
}
},
{
"name": "28-LPQ-TN3-QCOM-F1",
"node": {
"name": "28",
"id": 2
},
"timeline": {
"epc": 1349060400000,
"m1": null,
"m2": null,
"m3": 1285902000000,
"m4": 1325386800000,
"m5": 1357009200000,
"m6": 1372647600000,
"m7": 1388545200000
},
"fab": {
"name": "F1",
"id": 2
}
}];
Currently, all events created also begin with m3. Thanks for any help!
var
. Please don't use unintroduced variables likedata
. – Bergi Dec 18 '12 at 22:26return
statement ingetMilestone
seems wrong, and please get rid of the annoying repetitions ingetSecondStone
. – Bergi Dec 18 '12 at 22:27