15,996,252 members
See more:
I want to use the result of my first function "calsPerDay" as a variable in my second function. I currently have random numbers in the result for the second function, "myfunction." I can't figure out how to define the result of my first function as a variable to be used in the second.

What I have tried:

function calsPerDay(output)
{function find(id) { return document.getElementById(id)}
var age = find("age").value
var weight = find("weight").value
var bodyfat = find("bodyfat").value
var result = 0
if (find("Sedentary").checked)
result = (370 + ((21.6 * ((weight/2.20462) * (100 - bodyfat))) / 100 )) * 1.2
else if (find("Lightly Active").checked)
result = (370 + ((21.6 * ((weight/2.20462) * (100 - bodyfat))) / 100 )) * 1.375
else if (find("Moderately Active").checked)
result = (370 + ((21.6 * ((weight/2.20462) * (100 - bodyfat))) / 100 )) * 1.55
else if (find("Extremely Active").checked)
result = (370 + ((21.6 * ((weight/2.20462) * (100 - bodyfat))) / 100 )) * 1.725
find("calsPerDay").innerHTML = Math.round( result )
}
calsPerDay()

function myFunction()
{function find(id) { return document.getElementById(id)}

var age = find("age").value
var bodyfat = find("bodyfat").value
var result= 0 ;
if (bodyfat > 18 && (age <= 30) && (find("female").checked))
result = (500);
else if (bodyfat < 18 && (age = 31) && (find("female").checked))
result = (2+2);
else
result = ( 20 +1);

document.getElementById("macros").innerHTML = Math.round( result );
}
Posted
Updated 5-Mar-18 16:05pm

## Solution 1

try

```function find(id) { return document.getElementById(id) }

function calsPerDay( ) {

var age = parseFloat(find("age").value);
var weight = parseFloat(find("weight").value)
var bodyfat = parseFloat(find("bodyfat").value)
var result = 0
if (find("Sedentary").checked)
result = (370 + ((21.6 * ((weight / 2.20462) * (100 - bodyfat))) / 100)) * 1.2
else if (find("Lightly Active").checked)
result = (370 + ((21.6 * ((weight / 2.20462) * (100 - bodyfat))) / 100)) * 1.375
else if (find("Moderately Active").checked)
result = (370 + ((21.6 * ((weight / 2.20462) * (100 - bodyfat))) / 100)) * 1.55
else if (find("Extremely Active").checked)
result = (370 + ((21.6 * ((weight / 2.20462) * (100 - bodyfat))) / 100)) * 1.725
var calsperDay_Result  = Math.round(result)
find("calsPerDay").innerHTML = calsperDay_Result;
return calsperDay_Result;
}

function myFunction() {

var calsPDay = calsPerDay();

var age = parseFloat(find("age").value)
var bodyfat = parseFloat(find("bodyfat").value)
var result = 0;
if (bodyfat > 18 && (age <= 30) && (find("female").checked))
result = (500);
else if (bodyfat < 18 && (age = 31) && (find("female").checked))
result = (2 + 2);
else
result = (20 + 1);

document.getElementById("macros").innerHTML = Math.round(result);
}
```

use parseFloat() Function[^] to convert string to a float value for mathematical calculation.

v2