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.
Also, value
is unused, so it can be removed.
##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(j, sex, type) {
for (var i = j; i > 4; i--) {
if (data[sex][type].hasOwnProperty(i)){
return getcalc(
data[sex][type][i].m,
data[sex][type][i].l,
data[sex][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.