0

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

1
  • 2
    Can you please show how you use the returned values? Commented Mar 15, 2015 at 22:23

2 Answers 2

1

return [consume,burn]; will do exactly what you are asking it to do, so it seems likely that you are doing something wrong when you call the calories(...) function and then access the result.

You should do that like this:

var result = calories(...);
console.log(result[0]);     // consume value
console.log(result[1]);     // burn value
Sign up to request clarification or add additional context in comments.

5 Comments

Just for the note: with ES6 you can do it simply as let [consume,burn] = calories(...);
@c-smile - I wonder how long until we'll feel like we can use ES6 with impunity in client browser code and not worry about lack of browser support in some percentage of the user base? I guess server-side use and client use with a transpiler will come first.
I tried this and it returns 2101 0 into the console, so burn is always being returned as 0 still
@LucyWalsh - then the input to your function is not causing you to set the burn value appropriately. So, either the arguments you pass to your function or the logic in your function is not what it should be. Not much we can do without knowing exactly what you are passing to the function.
Just in case anyone ever stumbles across this question again, this ^^ was the problem! And the solution offered in the question worked a treat :)
0

probably the main reason is that you don't have proper input for your function. Please provide example input.

You should use if-else statement e.g.

if (gender==='F') {
  bmr = 655 + (4.3*weight) + (4.7*height) - (4.7*age);
} else {
  bmr = 66 + (6.3*weight) + (12.9*height) - (6.8*age);
}

Or if-else if-else structure with alert in else - when your input does not match to your ifs

if (gender==='F') {
  bmr = 655 + (4.3*weight) + (4.7*height) - (4.7*age);
} else if (gender==='M' {
  bmr = 66 + (6.3*weight) + (12.9*height) - (6.8*age);
} else {
  window.alert('wrong gender: ' + gender);
}

1 Comment

You have a typo in your second snippet. Please also focus on the question first. This answer should have been a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.