如何引用数组索引,将其解析为int,然后在JavaScript中为其添加条件语句?

时间:2014-07-10 17:43:38

标签: javascript arrays split conditional

我已经尝试过让这个条件语句工作一段时间了。如果我只是将数组索引写入页面,它会显示正确的值。但是,如果我将条件if,else if,else语句添加到我的代码中,我什么也得不到。

<html> 
<head> 
</head> 
<body> 
    The database Header values are:<br />
    <p id="headerValues"></p>

    <br />
    <br />  
    The array is:<br />     
    <div id="arrayDiv">12/1/2012,119,00119 - Henderson Dialysis Center,2325946.3,HD,4/17/2002,9/25/2000,51,4.2,1.67,Graft,9.6,7.6,251,Yes,Yes</div><br /> 
    <br />
    <br />
    The second array is:<br />  
    <div id="arrayDiv2">12/1/2012,119,00119 - Henderson Dialysis Center,2884094,HD,10/25/2005,10/25/2005,51,4.10,1.99,Fistula,10.00,3.10,461.00,Yes,No</div><br /> 
        <script>
            var headerValues = ["MM/DD/YYYY","Facility No","FacilityNoName","Number","Modality Group","FDODD","FDODE","Age At EOM","Albumin Result","sp Kt V Result","Access Result","Calcium Result","Phosphorous Result","PTH Result","Influenza Vaccination Result","Pneumococcal Vaccination Result"];
            var myTxt = document.getElementById('arrayDiv').innerHTML; 
            var myArr = myTxt.split(','); 
            var arrTxt = document.getElementById('arrayDiv2').innerHTML;
            var myArr2 = arrTxt.split(',');
                var albLevel = parseFloat(myArr[8]);



            for (var i=0; i<headerValues.length && myArr.length ; i+=1) { 
            document.writeln(headerValues[i].bold() + ':'.bold() + ' '+ myArr[i] + '<br />'); 
                if (albLevel < 3.4)  {
                document.writeln(albLevel) + ": The patient's Albumin level is low. Kidney disease is of greater risk for this individual.";
                }
                    else if (albLevel > 5.4) {
                    document.writeln(albLevel) + ": The patient's Albumin level is high. Tests should be performed to check for Dehydration or a high protien diet.";
                    }
                        else (albLevel >= 3.4 || <= 5.4) {
                        document.writeln(albLevel) + ": The patient's Albumin level is normal.";
                        } 
            }

        </script>
    <br />
    <br />
    <div id="normAlbRange">Normal Albumin Range: 3.4 - 5.4 grams per deciliter.</div><br />
    <div id="LowAlbRange">Low Albumin Range: < 3.4 grams per deciliter.</div><br />
    <div id="HighAlbRange">High Albumin Range: > 5.4 grams per deciliter.</div><br /><br />

    <b>The Patient's Albumin Level is:</b>

    <p id="output"></p>

</body> 
</html>

2 个答案:

答案 0 :(得分:0)

实际上,您的代码中出现了javascript错误。尝试切换|通过||当你想要OR运算符时。

[]的

答案 1 :(得分:0)

如果您注释掉的代码存在问题,请替换: (albLevel >= 3.4 | <= 5.4)(albLevel >= 3.4 || albLevel <= 5.4)