vendredi 3 mars 2017

If/Else nightmare. jQuery Optimization tips?

I have the following (example) jQuery code within a function that as you can see contains some awful if/else statements with a lot of repetition of sorts. Is there a better (more performance, DRY, readable) way of going about all these if/else statements?

var qux = "";

if (foo == 'a') {
    if (bar = 'x') {
        if (baz = 'm') {
            qux = 'axm';
        } else if (baz = 'n') {
            qux = 'axn';
        }
    } else if (bar = 'y') {
        if (baz = 'm') {
            qux = 'aym';
        } else if (baz = 'n') {
            qux = 'ayn';
        }
    } else if (bar = 'z') {
        if (baz = 'm') {
            qux = 'azm';
        } else if (baz = 'n') {
            qux = 'azn';
        }
    }
} else if (foo == 'b') {
    if (bar = 'x') {
        if (baz = 'm') {
            qux = 'bxm';
        } else if (baz = 'n') {
            qux = 'bxn';
        }
    } else if (bar = 'y') {
        if (baz = 'm') {
            qux = 'bym';
        } else if (baz = 'n') {
            qux = 'byn';
        }
    } else if (bar = 'z') {
        if (baz = 'm') {
            qux = 'bzm';
        } else if (baz = 'n') {
            qux = 'bzn';
        }
    }
} else if (foo == 'c') {
    if (bar = 'x') {
        if (baz = 'm') {
            qux = 'cxm';
        } else if (baz = 'n') {
            qux = 'cxn';
        }
    } else if (bar = 'y') {
        if (baz = 'm') {
            qux = 'cym';
        } else if (baz = 'n') {
            qux = 'cyn';
        }
    } else if (bar = 'z') {
        if (baz = 'm') {
            qux = 'czm';
        } else if (baz = 'n') {
            qux = 'czn';
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire