2016-12-06 2 views
-2

현재 javascript로 bmi를 계산할 수있는 프로그램을 작성 중입니다. 왜 그런지 잘 모르지만 제대로 작동하지 않습니다. 나는 무엇인가 놓치고 있어야한다. 그러나 나는 그것이 무엇인지에 관해 모른다. 누군가가 나를 도울 수 있다면 정말 감사 할 것입니다. 고맙습니다.javascript로 bmi를 계산하십시오.

<!DOCTYPE html> 
    <!-- --> 
<html> 

    <head> 
     <meta charset="UTF-8" /> 
     <title>Body Mass Index</title> 
    </head> 


    <BODY> 
    <header><img src="bmi.jpeg" width="380" height="132" border="0" alt="bmi"></header> 

<video controls="controls" 
width="320px" height="260px"> 
<source src="bmi.mp4"/> 

     <p>When it comes to weight loss, there's no lack of fad diets promising fast results. But such diets limit your nutritional intake, can be unhealthy, and tend to fail in the long run.</p> 

     <p>The key to achieving and maintaining a healthy weight isn't about short-term dietary changes. It's about a lifestyle that includes healthy eating, regular physical activity, and balancing the number of calories you consume with the number of calories your body uses.</p> 

     <p>BMI is a number calculated from a person's weight and height. BMI provides a reliable indicator of body fatness for most people and is used to screen for weight categories that may lead to health problems.</p> 

     <p>The BMI ranges are based on the relationship between body weight and disease and death. 
     Overweight and obese individuals are at increased risk for many diseases and health conditions, including the following:</p> 





You need a flash player to view this video. 

</video> 

     <ul> 
     <li>Hypertension</li> 
     <li>Dyslipidemia (for example, high LDL cholesterol, low HDL cholesterol, or high levels of triglycerides)</li> 
     <li>Type 2 diabetes</li> 
     <li>Coronary heart disease</li> 
     <li>Stroke</li> 
     <li>Gallbladder disease</li> 
     <li>Osteoarthritis</li> 
     <li>Sleep apnea and respiratory problems</li> 
     <li>Some cancers (endometrial, breast, and colon)</li> 
    </ul> 

<script type="text/javascript"> 
function CalculateBMI(){ 
var inch=12; 
var ft; 
var bmi= Math.write(weight*703)/ (inch height)^2; 

if(bmi<=19) 
{"Underweight"; 
} 
if else(19<bmi<=25) 
{"Desirable"; 
} 
if else(25<bmi<=29) 
{"Prone to health risks"; 
} 
if else (29<bmi<=40) 
{"obese" 
} 
else(40<bmi) 
{"Extremely Obese" 
} 
} 
</script> 
    <form name="bmi"> 


    <p> Weight:<p/></td> <td><input type="text" id="weight" name="weight" size="25" /> 


<p>Height:</p> <input type="text" id="textbox" name="textbox" size="25" /><td><p>Ft.</p><input type="text" id="textbox" name="textbox" size="25" /> <p>In.</p> 

<input type="submit" id="Calculate BMI" name="Calculate BMI" value="Calculate BMI" size="25" onclick="CalculateBMI()" /> 
According to the Panel on 
Energy, Obesity, and Body Weigth Standards published by 
American Journal of Clinical Nurttrition, your category is: 
<input type="text" id="textbox" name="textbox" size="25" /> 


    </form> 

    </BODY> 

</html> 
+0

죄송하지만 코드에 오류가 있습니다. 먼저 [브라우저 콘솔 열기] (http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers)에서 시작하여 구문 오류를 먼저 해결하십시오. – JJJ

답변

1

문제점을 유발할 수있는 몇 가지 사항이 있습니다. 이 문장으로 시작하자 : 당신은 중량 높이를 정의하지 않습니다

var bmi= Math.write(weight*703)/(inch height)^2; 

(당신은 텍스트 상자에보고하거나 자동으로 텍스트 상자를 참조 알고하지 않는 함수에 보내도록 말할 필요). 내가

var weight = document.getElementById('weight').value; 

같은 구문 오류를 던지고있다 신장과 체중 사이에 기호 없다 기대, 당신은이 추가되지 실현 그들이 (함께 할하려는 경우이 뭔가를 할 필요합니까 인치는 단지 피트로 계산합니다).

var bmi = (weight * 703)/(inch * height)^2;

그 후 당신이 경우 다른 사용 - 자바 스크립트에서 유효하지 않은 당신이 말하고 싶은 것 :

else if (19<bmi<=25) 

이 마지막으로 당신이 값을 반환하거나 값이 가야 WHERE 지정되지 않습니다.

var results; 
if (bmi<=19) 
{ 
    results = "Underweight" 
} 

document.getElementById('results').value = results; 

이러한 제안 사항 중 일부를 구현해보고 올바른 방향으로 이동하는지 확인해보십시오.

0

자바 스크립트 예를 1<a<12 몇 전신 오류를

을 가지고하는 것은 유효하지 않습니다, 그것은

a>1 && a<12를 작성해야합니다 또한 당신의 CalculateBMI 기능은 몰랐다 무엇을 발에 체중 인치 높이 또는 높이입니다. 이것을 쉽게하기 위해 JQuery을 포함 할 수 있지만 document.getElementById도 사용할 수 있습니다. 이를 달성하기 위해 양식 변수에 의미있는 이름을 붙였습니다.

결과 문자열을 어디에도 표시하지 않았습니다. getElementById을 사용하면 html에서 result 요소를 찾고 해당 값을 계산 결과로 설정할 수 있습니다.

제출할 때 페이지를 새로 고치지 않으려면 falseonsubmit을 받도록 양식을 설정해야합니다.

bmi에 대한 수학 구문이 꺼져 있었는데, 높이가 인치 (피트 및 인치 높이 변수가 결합 된 후) 인 곳에 (703 * 무게) (높이)^2이어야한다고 가정합니다.

정리 된 Javascript는 다음과 같아야합니다. 이 문제를 해결할 수있는 최선의 방법은 아닐 것입니다.

편집 : 당신의 BMI 계산도 꺼져 있다고 생각합니다. 입력 무게와 높이가 제국 (즉, 인치와 파운드)이라면 인치에 0.025를 곱해서 미터를 얻고 lbs에 0.45를 곱해서 kgs를 얻어야합니다.

bmi= (weight_e* 0.45)/(((12*heightf_e)+heighti_e) * 0.025)^2;

function CalculateBMI(){ 
    // To avoid erros due to hoisting define all 
    // of your vars at the top of your function  
    var ft, 
    bmi, 
    heighti_e, // height in inches 
    heightf_e, // height in feat 
    weight_e, 
    bmi_e, // the bmi element 
    bmi;     
    // Use getElementById to get a reference for the 
    // elements you will be using in your function 
    heighti_e=document.getElementById("heighti"); 
    heightf_e=document.getElementById("heightf"); 
    weight_e=document.getElementById("weight"); 
    bmi_e=document.getElementById("bmi"); 
    // Not all of these parenthesis are necessary but it 
    // helps to clear up the order of operation and avoid 
    // silly mistakes in long equations that are not 
    // broken up into several lines 
    bmi= (weight_e* 0.45)/(((12*heightf_e)+heighti_e) * 0.025)^2; 
    // set bmi to a string value 
    if(bmi<=19) 
    { 
    bmi="Underweight"; 
    } 
    else if(bmi>19 && bmi<=25) 
    { 
    bmi="Desirable"; 
    } 
    else if(bmi>25 && bmi<=29) 
    { 
    bmi="Prone to health risks"; 
    } 
    else if (bmi>29 && bmi<=40) 
    { 
    bmi="obese"; 
    } 
    else(bmi>40) 
    { 
    bmi="Extremely Obese"; 
    } 
    bmi_e.value=bmi; // bmi_a is the reference to the 
        // element in your form with the 
        // bmi id 
    return false; // make sure you return false to prevent 
        // page reload 
} 

나는 당신의 HTML 양식을 정리하지 않은,하지만 적어도 지금은 작동합니다. 제출 작업을 버튼에서 form 태그로 옮겼습니다. 높이 웨이트와 bmi 입력에 의미있는 ID 이름을 부여하여 CalculateBMI 함수에서 참조 할 수 있습니다.

<form action="" name="bmi" onsubmit="return CalculateBMI()"> 
    <p> Weight:<p/></td> <td><input type="text" id="weight" name="weight" size="25" /> 

    <p>Height:</p> <input type="text" id="heightf" name="textbox" size="25" /><td><p>Ft.</p><input type="text" id="heighti" name="textbox" size="25" /> <p>In.</p> 

    <input type="submit" id="Calculate BMI" name="Calculate BMI" value="Calculate BMI" size="25" /> 
    According to the Panel on 
    Energy, Obesity, and Body Weigth Standards published by 
    American Journal of Clinical Nurttrition, your category is: 
    <input type="text" id="bmi" name="textbox" size="25" /> 
</form> 
+0

감사합니다. 이것은 많은 도움이됩니다. 그것은 내가했던 것보다 자바 스크립트를 훨씬 더 잘 이해할 수있게 도와 주지만 결과에 관계없이 나는 극도로 비만하다고 말했다. – 11ash123

관련 문제