2013-04-22 18 views
-6

我正在使用Javascript構建一個風險計算器程序,爲此,我有一個1-3的規模,其中零是沒有風險,三個是最高風險。對於每個選項(年齡,性別,BMI,血壓,體溫,吸菸等),我將分配一個數字(0-3)並使用該數字來確定風險百分比。如何在Javascript中分配數字?

實施例:年齡:

18-39 = 0風險

40+ = 2風險

無論用戶選擇他們的年齡將增加他們的個人百分比。

我該如何編寫這個程序?

List<StringWithValue> stringList == new ArrayList<StringWithValue>(); 
    int myRisk; 
    // no risk = "0"; 
    //some risk = "1"; 
    // risk = "2"; 
    //abnormal risk = "3"; 
    var a = "0", b = "1", c = "2", d = "3" 


    if(message == "Age") { 
     alert("Ages 18 to 39 have a lower risk of cancer" + '\n\n' + 
     "Than for those over 40."); 

     StringAge = "18-39"; 
     StringValue = "a"; 
     System.out.println("Age" = "a"); 


     StringAge_2 = "40-80"; 
     StringValue = "c"; 
     System.out.println("Age_2" = "c"); 


    } else if(message =="Sex") { 
     alert("Choose your biological gender" + '\n\n' + "Males are more likely to develop" + 
      " lung cancer than females."); 

    } else if(message == "Systolic Blood Pressure") { 
      alert("Low blood pressure is anything below 100" + 'n\n' + "Normal range is 120-130" + 
    "High blood pressure is over 135."); 

    } else if(message =="Diastolic Blood Pressure") { 
     alert("Low blood pressure is anything below 70" + 'n\n' +"Normal range is 80-85" + 
    "High blood pressure is over 90."); 

    } else if(message =="Temperature") { 
     alert("Below 98.6 degrees Fahrenheit/37 degrees Celsius is unhealthy" + 'n\n' + "Normal range is at exactly 98.6 degrees/37 degrees Celsius" + 
    "Anything above 98.6 degrees Fahrenheit/37 degrees Celsius is abnormal."); 

    } else if(message =="Race") { 
     alert("African Americans have a higher chance of lung cancer" + 'n\n' + "Followed by Caucasians" + 
    "Asians, Pacific Islanders, Hispanics, and Native Americans have a low percentage."); 


    } 
    else if(message == "family") { 
     alert("Family History of Lung Cancer" + '\n\n' + 
     "Choose Yes if an immediate family member had " + 
     "lung cancer."); 

     "Note: The Calculator is only applicable for persons without a previous diagnosis of lung cancer."); 

    } 
} 

function radiobtnchange(units){ 
    if(units == "Standard"){ 
     document.getElementById('lblHeightUnits').innerHTML = "in"; 
     document.getElementById('lblWeightUnits').innerHTML = "lbs"; 
    } 
    if(units == "Metric"){ 
     document.getElementById('lblHeightUnits').innerHTML = "cm"; 
     document.getElementById('lblWeightUnits').innerHTML = "kg"; 
    } 
} 

function clearAllFields(theForm) { 
    if(theForm.age) clearObjValue(theForm.race); 
    if(theForm.sex) clearObjValue(theForm.pca3); 
    if(theForm.systolic_blood_pressure) clearObjValue(theForm.free_psa); 
    if(theForm.diastolic_blood_pressure) clearObjValue(theForm.pro_psa); 
    if(theForm.bmi) clearObjValue(theForm.height); 
    if(theForm.temperature) clearObjValue(theForm.weight); 
    if(theForm.prostate_volume) clearObjValue(theForm.prostate_volume); 
    if(theForm.num_biopsy_cores) clearObjValue(theForm.num_biopsy_cores); 
    if(theForm.aua_symptom_score) clearObjValue(theForm.aua_symptom_score); 
    if(theForm.age) clearObjValue(theForm.age); 
    if(theForm.psa) clearObjValue(theForm.psa); 
    if(theForm.familyhistory) clearObjValue(theForm.familyhistory); 
    if(theForm.dre) clearObjValue(theForm.dre); 
    if(theForm.biopsy) clearObjValue(theForm.biopsy); 
    if(theForm.finasteride) clearObjValue(theForm.finasteride); 
+1

也就是說* JavaScript的*代碼,它是不一樣的*的Java *。 – Blorgbeard 2013-04-22 00:07:07

+1

一個不會簡單地混合Java和JavaScript代碼。 – 2013-04-22 00:27:31

回答

0

我爲您設置了jsfiddle。您可以插入邏輯來計算剩餘字段的風險並清理html。

的JavaScript

var risk = { 
    'none': 0, 
    'low': 1, 
    'high': 2, 
    'abnormal': 3 
}; 

var myRsk = 0; 

function getValue(name) { 
    return document.getElementsByName(name)[0].value; 
} 

function calculateRisk() { 
    age = getValue('iAge'); 
    gender = getValue('iGender'); 
    sbp = getValue('iSBP'); 
    dbp = getValue('iDBP'); 
    temp = getValue('iTemp'); 
    race = getValue('iRace'); 
    family = getValue('iFamily'); 

    myRisk = 0; 
    myRisk += calculateAge(age); 
    myRisk += calculateGender(gender); 
    myRisk += calculateSBP(sbp); 
    myRisk += calculateDBP(sbp); 
    myRisk += calculateTemp(temp); 
    myRisk += calculateRace(race); 
    myRisk += calculateFamily(family); 
    myRisk = parseFloat(myRisk/21 * 100).toPrecision(4); 

    alert('Your Risk = ' + myRisk + '%'); 
} 

function calculateAge(age) { 
    val = parseInt(age) 
    if (val > 80) return risk['abnormal']; 
    else if (val > 60) return risk['high']; 
    else if (val > 40) return risk['low']; 
    else return parseInt(risk['none']); 
} 

function calculateGender(gender) { 
    return parseInt(risk['low']); 
} 

function calculateSBP(sbp) { 
    return parseInt(risk['low']); 
} 

function calculateDBP(dbp) { 
    return parseInt(risk['low']); 
} 

function calculateTemp(temp) { 
    return parseInt(risk['low']); 
} 

function calculateRace(race) { 
    return parseInt(risk['low']); 
} 

function calculateFamily(family) { 
    return parseInt(risk['low']); 
} 

HTML

<form> 
    <div class="item" title="Ages 18 to 39 have a lower risk of cancer than for those over 40."> 
     <label>Age</label> 
     <input name="iAge" type="text" /> 
    </div> 
    <div class="item" title="Choose your biological gender. Males are more likely to develop lung cancer than females."> 
     <label>Gender</label> 
     <input name="iGender" type="text" /> 
    </div> 
    <div class="item" title="Low blood pressure is anything below 100. Normal range is 120-130. High blood pressure is over 135."> 
     <label>Systolic Blood Pressure</label> 
     <input name="iSBP" type="text" /> 
    </div> 
    <div class="item" title="Low blood pressure is anything below 70. Normal range is 80-85. High blood pressure is over 90."> 
     <label>Diastolic Blood Pressure</label> 
     <input name="iDBP" type="text" /> 
    </div> 
    <div class="item" title="Below 98.6 degrees Fahrenheit/37 degrees Celsius is unhealthy Normal range is at exactly 98.6 degrees/37 degrees Celsius"> 
     <label>Temperature</label> 
     <input name="iTemp" type="text" /> 
    </div> 
    <div class="item" title="African Americans have a higher chance of lung cancer; followed by Caucasians, Asians, Pacific Islanders, Hispanics, and Native Americans have a low percentage."> 
     <label>Race</label> 
     <input name="iRace" type="text" /> 
    </div> 
    <div class="item" title="Family History of Lung Cancer. Choose Yes if an immediate family member had lung cancer. Note: The Calculator is only applicable for persons without a previous diagnosis of lung cancer."> 
     <label>Family</label> 
     <input name="iFamily" type="text" /> 
    </div> 
    <input type="button" value="Calculate" onclick="calculateRisk()" /> 
</form> 

CSS

label { 
    width: 160px; 
    float: left; 
} 
input[type='text'] { 
    vertical-align:middle; 
} 
.item { 
    margin-bottom:1.5em; 
    clear:both; 
    overflow:hidden; 
} 
相關問題