简体   繁体   中英

How can I reference an array index, parse it to an int, and then put a conditional statement on it in JavaScript?

I have tried getting this conditional statement to work for sometime. If I simply write the array index to the page, it shows the correct value. However, if I add the conditional if, else if, else statement into my code, I get nothing.

<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>

Actually, you got a javascript error in your code. Try to switch | by || when you want OR operator.

[]'s

如果您的注释代码有问题,则将(albLevel >= 3.4 | <= 5.4)替换为(albLevel >= 3.4 || albLevel <= 5.4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM