Javascript - Replace lots of if statements
code-cleanup, if-statement, javascript, refactoring
Solution
I'm not sure if this is to your taste, but it seems like a cleaner solution to me. Lay out all the mappings separately first. Then resolve the mappings to the correct property of parent. Then invoke validation, just once, for the property you've retrieved. Here's a sketch:
// map var1 and var2 to gender and ethnicity
var genderMap = { "7707330" : "boy", "7707333": "girl", "7707336": "both" };
var ethnicityMap = { "7707341": "maori", "7707344": "pasifica", "7707347": "all" };
// map gender and ethnicity to the correct property of "parent"
var props =
{ "boy":
{
"maori": "getMBoys", "pacifica": "getPBoys", "all": "getBoys"
},
"girl":
{
"maori": "getMGirls", "pacifica": "getPGirls", "all": "getGirls"
},
"all":
{
"maori": "getTotalM", "pacifica": "getTotalP", "all": "getTotalRoll"
}
};
// get the correct property
var prop = props[genderMap[var1]][ethnicityMap[var2]];
// run the validation
validation(parseFloat(parent[prop]));
Problem
I have a a couple of different radio buttons which return an ethnicity and a gender. The script runs inside an internal application so rather than returning "boy", "girl" or "both" I get back 7707330, 7707333, and 7707336. Similar from the ethnicity radio button. I then need to validate data based on the combonation of ethnicity and gender. This was a pretty simple task but I have ended up with 15 if statements! It all works as it should, but there must be a cleaner solution? ``` function test(radioResults) { var1 = radioResults[0].toString(); var2 = radioResults[1].toString(); var roll = parseFloat(parent.roll); if (var2 == '7707330') { gender = 'boy'; } if (var2 == '7707333') { gender = 'girl'; } if (var2 == '7707336') { gender = 'both'; } if (var1 == '7707341') { maori(gender); } if (var1 == '7707344') { pasifika(gender); } if (var1 == '7707347') { all(gender); } } function maori(gender) { //Maori if (gender == 'boy') { ethnicity = parseFloat(parent.getMBoys); validation(ethnicity); } if (gender == 'girl') { ethnicity = parseFloat(parent.getMGirls); validation(ethnicity); } if (gender == 'both') { ethnicity = parseFloat(parent.getTotalM); validation(ethnicity); } } function pasifika(gender) { //Pasifika if (gender == 'boy') { ethnicity = parseFloat(parent.getPBoys); validation(ethnicity); } if (gender == 'girl') { ethnicity = parseFloat(parent.getPGirls); validation(ethnicity); } if (gender == 'both') { ethnicity = parseFloat(parent.getTotalP); validation(ethnicity); } } function all(gender) { //All if (gender == 'boy') { ethnicity = parseFloat(parent.getBoys); validation(ethnicity); } if (gender == 'girl') { ethnicity = parseFloat(parent.getGirls); validation(ethnicity); } if (gender == 'both') { ethnicity = parseFloat(parent.getTotalRoll); validation(ethnicity); } } function validation(ethnicity) { percent = ethnicity * 5 / 100; if (ethnicity - percent > roll || (ethnicity + percent < roll)) { parent.document.getElementById('CF_7784443').value = "FAIL"; } else { parent.document.getElementById('CF_7784443').value = "PASS"; } } ``` How would I go about cleaning this up?