I'm coding a function in Javacsript that returns two variables, Consume and Burn. Here is the function:
function calories(goal, goalWeight, activityLevel, days, gender, age, height, weight){
var bmr=0;
var consume=0;
var burn=0;
if (gender==='F'){
bmr = 655 + (4.3*weight) + (4.7*height) - (4.7*age);
}
if (gender==='M'){
bmr = 66 + (6.3*weight) + (12.9*height) - (6.8*age);
}
if (activityLevel==='Inactive'){
consume=bmr+(0.2*bmr);
}
if (activityLevel==='Relatively Active'){
consume=bmr+(0.3*bmr);
}
if (activityLevel==='Very Active'){
consume=bmr+(0.4*bmr);
}
if (goal==='Lose'){
if((goalWeight/days)<=0.06){
consume=consume-200;
}
if((goalWeight/days)<=0.15){
consume=consume-100;
}
if (activityLevel==='Inactive'){
burn=250;
}
if (activityLevel==='Relatively Active'){
burn=150;
}
if (activityLevel==='Very Active'){
burn=100;
}
}
if(goal==='Gain'){
if((goalWeight/days)<=0.06){
consume=consume+200;
}
if((goalWeight/days)<=0.15){
consume=consume+100;
}
if (activityLevel==='Inactive'){
burn=200;
}
if (activityLevel==='Relatively Active'){
burn=100;
}
if (activityLevel==='Very Active'){
burn=100;
}
}
if(goal==='Maintain'){
burn=100;
}
consume=Math.round(consume);
window.alert(consume);
window.alert(burn);
return [consume,burn];
}
In the window box, the function returns the correct value for consume every time, but for some reason burn is always returned as zero. If I don't declare it outside of the if statements, it is returned as undefined. Is there some reason that I cannot assign numbers to this variable inside of the if statements? I have used window.alert() to test if the correct if statement is being accessed and each time it returns what I wanted, but burn is never assigned a value, I have no idea why! Thanks for any advice
EDIT Thanks for the answers so far. An example input is :
var calories = calories(goal, healthyWeight, activityLevel, days, gender, age, height, weight);
where:
goal = 'Lose'
healthyWeight = 128
activityLevel = 'Inactive'
days = 180
gender = 'M'
age = 20
height = 63
weight = 160
This returns consume as 2101 as expected, and returns burn as 0
At the moment all I'm doing with the returned values is this:
window.alert(consume);
window.alert(burn);
to test what values they are, but once the function is working properly I'll be using
var consume=calories.consume;
var burn=calories.burn;
document.getElementById("consume").innerHTML = consume;
document.getElementById("burn").innerHTML = burn;
outside of the function to display them in the browser