Click here to Skip to main content
15,912,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

1 solution

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900