-2

I am trying to make a calculator that calculates the amount of calories that have been burned in a certain amount of time, but whenever I run it, I get 0 as the output. I have tried setting the calorieCountOut variable to an arbitrary number, and then it works fine, but every time I run it with this code here, I get 0. Here is my code:

const AGECONST = 0.2017;
const WEIGHTCONST = 0.09036;
const HRCONST = 0.6309;
const SUBTRACTCONST = 55.0969;
const TIMECONST = 4.184;

//var gender = document.getElementById("gender").innerHTML;
var gender = "male";
var weight = document.getElementById("weight");
var age = document.getElementById("age");
var time = document.getElementById("time");
var hr = 140;//dummy number

function calculate(){
if (gender = "male"){
var calorieCount = ((age * AGECONST) - (weight * WEIGHTCONST) + (hr * HRCONST) - SUBTRACTCONST) * time / TIMECONST;
}
//else if (gender = "female"){

//}

var calorieCountOut = calorieCount.toString();

document.getElementById("output").innerHTML = calorieCountOut;

}
  • Make a console.log of each variable. It seems that an input isn't good – IsraGab Apr 10 '16 at 04:13
  • You're assigning elements to weight, age, and time, you'd need to do something like `.value` or `.innerHTML` to get an actual value. Keep in mind the value would be a string, but as long as you're using the multiplication operator, there's no need to type cast it to a number with something like parseInt or parseFloat. – Ultimater Apr 10 '16 at 04:18
  • 1
    `if (gender = "male")` will always be true. – Sebastian Simon Apr 10 '16 at 04:18
  • Also `calorieCount` is declared local to your calculate() function, try removing the "var" from it if you need to access it from outside of the function. – Ultimater Apr 10 '16 at 04:20
  • Walk through your code with a debugger. At the line `var weight`, stop and examine the value of the `weight` variable. –  Apr 10 '16 at 06:02

1 Answers1

-1

Try getElementById('myId').value for the value inside the control instead of the control itself as an object.

Right now you assign a html object to a variable, but what you want (i assume) is the number stored in that html object.

nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • I call the function in an HTML file. It works when I set the weight, age, and time variables to numbers, I think the variables may be coming in as strings, and I don't know how to make them numbers/doubles. – Kyle Habig Apr 10 '16 at 04:18
  • @KyleHabig you're right - edited – nhouser9 Apr 10 '16 at 04:21
  • Also, when I use .innerHTML, I get NaN as a result. How do I convert the string values to a number value? – Kyle Habig Apr 10 '16 at 04:26
  • @KyleHabig See my edited answer. If you have HTML text fields, you don't want the inner html, you want the element.value. – nhouser9 Apr 10 '16 at 04:26