Skip to main content
added 1 character in body; edited title
Source Link
Quill
  • 11.9k
  • 5
  • 40
  • 93

Simplify function with repeating if Sorting variables for human weight/else partslength calculations

I'm trying to simplify this function, as there can be multiple type of data objctsobjects and for each type there is also a male and a female version. The number and name of the elements in the objects are always identical.

Simplify function with repeating if/else parts

I'm trying to simplify this function, as there can be multiple type of data objcts and for each type there is also a male and a female version. The number and name of the elements in the objects are always identical.

Sorting variables for human weight/length calculations

I'm trying to simplify this function, as there can be multiple type of data objects and for each type there is also a male and a female version. The number and name of the elements in the objects are always identical.

Source Link

Simplify function with repeating if/else parts

I'm trying to simplify this function, as there can be multiple type of data objcts and for each type there is also a male and a female version. The number and name of the elements in the objects are always identical.

As you see, most of the code is repeating...

function calculate(type, j, value, s) {
    for (var i = j; i > 4; i--) { 
        if (type == 'weight') {
            if (s == 'f') { 
                if (weightFemale.hasOwnProperty(i)) {
                    var m = weightFemale[i][0],
                        l = weightFemale[i][1],
                        s = weightFemale[i][2];     
                    return getcalc( m,l,s );
                }
            }
            else {
                if (weightMale.hasOwnProperty(i)) {
                    var m = weightMale[i][0],
                        l = weightMale[i][1],
                        s = weightMale[i][2];       
                    return getcalc( m,l,s );
                }               
            }
        }
        else if (type == 'length') {
            if (s == 'f') { 
                if (lengthFemale.hasOwnProperty(i)) {
                    var m = lengthFemale[i][0],
                        l = lengthFemale[i][1],
                        s = lengthFemale[i][2],
                    return getcalc( m,l,s );
                }
            }
            else {
                if (lengthMale.hasOwnProperty(i)) {
                    var m = lengthMale[i][0],
                        l = lengthMale[i][1],
                        s = lengthMale[i][2],
                    return getcalc( m,l,s );
                }               
            }
        }
    }
    return false;
}

How can I simplify the if/else-parts for the type and the sex?