vendredi 17 août 2018

Avoiding several nested if statements

I'm writing a function that pushes specific (predefined) variables to predefined arrays based on the status of 4 variables, all of which are ranging from 1 to 5 (they are the results of radio buttons pressed on our web page).

If the variables equal A, B, C and D, the predefined variables to predefined arrays X, Y and Z are defined by:

X = the combination of A and B.

Y = the combination of C and D.

Z = the combination of A, B, C and D.

Below is my realization of a solution to said problem (A, B, C and D are motivation, orientation, influence and relevance), using nested if/else statements. However, I find this solution inelegant. I both want to write cleaner code, and leave the code more readable for my fellow coworkers. What would be the most elegant way to type this out? Should I use functions, switch statements, something else?

Below is the entire function:

function getRadioParameters (influence, relevance, motivation, orientation) {
    if (influence >= 3) {
        if (relevance >= 3) {
            influenceRelevanceArr.push(highInfluenceHighRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('HHHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('HHHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('HHLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('HHLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else if (relevance < 3) {
            influenceRelevanceArr.push(highInfluenceLowRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('HLHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('HLHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('HLLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('HLLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else {
            influenceRelevanceArr.push('');
            motivationOrientationArr.push('');
            stkImagesArr.push('');
            console.log('problem with relevance. It is =', relevance);
        }
    }
    else if (influence < 3) {
        if (relevance >= 3) {
            influenceRelevanceArr.push(lowInfluenceHighRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('LHHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('LHHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('LHLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('LHLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else if (relevance < 3) {
            influenceRelevanceArr.push(lowInfluenceLowRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('LLHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('LLHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('LLLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('LLLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else {
            influenceRelevanceArr.push('');
            motivationOrientationArr.push('');
            stkImagesArr.push('');
            console.log('problem with relevance. It is =', relevance);
        }
    }

}

Thank you!

Aucun commentaire:

Enregistrer un commentaire