Skip to main content
1 of 2
Quill
  • 11.9k
  • 5
  • 40
  • 93

First, you've got a few errors in the following:

    else if (type == 'length') {
        if (s == 'f') { 
            if (lengthFemale.hasOwnProperty(i)) {
                var m = lengthFemale[i][0],
                    l = lengthFemale[i][1],
                    s = lengthFemale[i][2], // <--- here
                return getcalc( m,l,s );
            }
        }
        else {
            if (lengthMale.hasOwnProperty(i)) {
                var m = lengthMale[i][0],
                    l = lengthMale[i][1],
                    s = lengthMale[i][2],  // <--- here
                return getcalc( m,l,s );

When you're redacting the var keyword by comma, you need to finish with a semicolon.

Also, in return getcalc( m,l,s ) your spacing is wrong. You don't need extraneous space in your brackets, and you should have a single whitespace after your commas.


##Data Structures:

At current, your data structure looks like the following:

  • weightFemale, weightMale, lengthFemale, lengthMale: two dimensional arrays

Instead of using arrays, use objects instead:

With a data structure like the following:

var data = {
    female: {
        weight: [
            {m: 4, l: 5, s: 7},
            {m: 4, l: 5, s: 7}
        ],
        length: [
            {m: 4, l: 5, s: 7}
        ]
    },
    male: {
        weight: [
            {m: 4, l: 5, s: 7},
            {m: 4, l: 5, s: 7}
        ],
        length: [
            {m: 4, l: 5, s: 7}
        ]
    },
}

Then, assuming you pass s in as female or male instead of f/m:

The following should work:

function calculate(type, j, value, s) {
    for (var i = j; i > 4; i--) {
        if (data[s][type].hasOwnProperty(i)){
            return getcalc(
                data[s][type][i].m,
                data[s][type][i].l,
                data[s][type][i].s
            );
        }
    }
    return false;
}

##Clarity:

Your program is unclear, the variables are named with single letters, which is bad practice as it is unclear and potentially hard to maintain in the future.

Consider using entire words instead of letters.

Quill
  • 11.9k
  • 5
  • 40
  • 93