如何将第一个函数的结果定义为在另一个函数中使用的变量? [英] How do I define the result of my first function as a variable to be used in another?
问题描述
我想在第二个函数中使用我的第一个函数calsPerDay的结果作为变量。我目前在第二个函数myfunction的结果中有随机数。我无法弄清楚如何将我的第一个函数的结果定义为在第二个函数中使用的变量。
我尝试了什么:
函数calsPerDay(输出)
{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)。 )
结果=(370 +((21.6 *((重量/ 2.20462)*(100 - bodyfat)))/ 100))* 1.375
否则如果(找到(中等活跃)。已检查)
结果=(370 +((21.6 *((重量/2.20462)*(100 - 体脂)))/ 100))* 1.55
else if(find(Extremely Active)。checked)
result =(370 +((21.6 *((重量/2.2) 0462)*(100 - bodyfat)))/ 100))* 1.725
find(calsPerDay)。innerHTML = Math.round(result)
}
calsPerDay()
< br $>
函数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);
}
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 );
}
推荐答案
试试
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);
}
使用 parseFloat()函数 [ ^ ]将字符串转换为浮点值以进行数学计算。
use parseFloat() Function[^] to convert string to a float value for mathematical calculation.
这篇关于如何将第一个函数的结果定义为在另一个函数中使用的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!