##Some points
Some points
- Put magic numbers in constants to give them meaning.
- Variables that do not change should be defined as constants
- Use appropriate Array functions rather than iterating over the array.
Math.round
only takes one argument. I do not know why you pass 2 as a second argument. If I guess you want to round to 2 decimal places. To do that you can useNumber.toFixed()
however that returns the number as a String so to be safe and return the same type you would useNumber(BMI.toFixed(2))
or you can useMath.round(BMI * 100) / 100
- You can use the
**
operator to raise a value to a power.
##Examples
Examples
const computeBMI = (weight, height, country) => {
const FEET_2_METERS = 0.3048;
const BMI_SCALE = 0.82;
const COUNTRIES = ["Chad", "Sierra Leone", "Mali", "Gambia", "Uganda", "Ghana", "Senegal", "Somalia", "Ivory Coast", "Isreal"];
return Math.round(weight / (height * FEET_2_METERS) ** 2 *
(COUNTRIES.includes(country) ? BMI_SCALE : 1));
};
Though it would be better to have the constants held in closure if you are to use them in other functions.
const compute = (()=> {
const FEET_2_METERS = 0.3048;
const BMI_SCALE = 0.82;
const COUNTRIES = ["Chad", "Sierra Leone", "Mali", "Gambia", "Uganda", "Ghana", "Senegal", "Somalia", "Ivory Coast", "Isreal"];
return {
BMI(weight, height, country) {
return Math.round(weight / (height * FEET_2_METERS) ** 2 * (COUNTRIES.includes(country) ? BMI_SCALE : 1));
}
};
})();
compute.BMI(90, 6.2, "Somewhere");
##Some points
- Put magic numbers in constants to give them meaning.
- Variables that do not change should be defined as constants
- Use appropriate Array functions rather than iterating over the array.
Math.round
only takes one argument. I do not know why you pass 2 as a second argument. If I guess you want to round to 2 decimal places. To do that you can useNumber.toFixed()
however that returns the number as a String so to be safe and return the same type you would useNumber(BMI.toFixed(2))
or you can useMath.round(BMI * 100) / 100
- You can use the
**
operator to raise a value to a power.
##Examples
const computeBMI = (weight, height, country) => {
const FEET_2_METERS = 0.3048;
const BMI_SCALE = 0.82;
const COUNTRIES = ["Chad", "Sierra Leone", "Mali", "Gambia", "Uganda", "Ghana", "Senegal", "Somalia", "Ivory Coast", "Isreal"];
return Math.round(weight / (height * FEET_2_METERS) ** 2 *
(COUNTRIES.includes(country) ? BMI_SCALE : 1));
};
Though it would be better to have the constants held in closure if you are to use them in other functions.
const compute = (()=> {
const FEET_2_METERS = 0.3048;
const BMI_SCALE = 0.82;
const COUNTRIES = ["Chad", "Sierra Leone", "Mali", "Gambia", "Uganda", "Ghana", "Senegal", "Somalia", "Ivory Coast", "Isreal"];
return {
BMI(weight, height, country) {
return Math.round(weight / (height * FEET_2_METERS) ** 2 * (COUNTRIES.includes(country) ? BMI_SCALE : 1));
}
};
})();
compute.BMI(90, 6.2, "Somewhere");
Some points
- Put magic numbers in constants to give them meaning.
- Variables that do not change should be defined as constants
- Use appropriate Array functions rather than iterating over the array.
Math.round
only takes one argument. I do not know why you pass 2 as a second argument. If I guess you want to round to 2 decimal places. To do that you can useNumber.toFixed()
however that returns the number as a String so to be safe and return the same type you would useNumber(BMI.toFixed(2))
or you can useMath.round(BMI * 100) / 100
- You can use the
**
operator to raise a value to a power.
Examples
const computeBMI = (weight, height, country) => {
const FEET_2_METERS = 0.3048;
const BMI_SCALE = 0.82;
const COUNTRIES = ["Chad", "Sierra Leone", "Mali", "Gambia", "Uganda", "Ghana", "Senegal", "Somalia", "Ivory Coast", "Isreal"];
return Math.round(weight / (height * FEET_2_METERS) ** 2 *
(COUNTRIES.includes(country) ? BMI_SCALE : 1));
};
Though it would be better to have the constants held in closure if you are to use them in other functions.
const compute = (()=> {
const FEET_2_METERS = 0.3048;
const BMI_SCALE = 0.82;
const COUNTRIES = ["Chad", "Sierra Leone", "Mali", "Gambia", "Uganda", "Ghana", "Senegal", "Somalia", "Ivory Coast", "Isreal"];
return {
BMI(weight, height, country) {
return Math.round(weight / (height * FEET_2_METERS) ** 2 * (COUNTRIES.includes(country) ? BMI_SCALE : 1));
}
};
})();
compute.BMI(90, 6.2, "Somewhere");
##Some points
- Put magic numbers in constants to give them meaning.
- Variables that do not change should be defined as constants
- Use appropriate Array functions rather than iterating over the array.
Math.round
only takes one argument. I do not know why you pass 2 as a second argument. If I guess you want to round to 2 decimal places. To do that you can useNumber.toFixed()
however that returns the number as a String so to be safe and return the same type you would useNumber(BMI.toFixed(2))
or you can useMath.round(BMI * 100) / 100
- You can use the
**
operator to raise a value to a power.
##Examples
const computeBMI = (weight, height, country) => {
const FEET_2_METERS = 0.3048;
const BMI_SCALE = 0.82;
const COUNTRIES = ["Chad", "Sierra Leone", "Mali", "Gambia", "Uganda", "Ghana", "Senegal", "Somalia", "Ivory Coast", "Isreal"];
return Math.round(weight / (height * FEET_2_METERS) ** 2 *
(COUNTRIES.includes(country) ? BMI_SCALE : 1));
};
Though it would be better to have the constants held in closure if you are to use them in other functions.
const compute = (()=> {
const FEET_2_METERS = 0.3048;
const BMI_SCALE = 0.82;
const COUNTRIES = ["Chad", "Sierra Leone", "Mali", "Gambia", "Uganda", "Ghana", "Senegal", "Somalia", "Ivory Coast", "Isreal"];
return {
BMI(weight, height, country) {
return Math.round(weight / (height * FEET_2_METERS) ** 2 * (COUNTRIES.includes(country) ? BMI_SCALE : 1));
}
};
})();
compute.BMI(90, 6.2, "Somewhere");