Brandon Zwicker
Brandon Zwicker

Reputation: 13

Javascript not working for calculation

Im creating a web app to help diabetics like myself. I am having trouble with a javascript that will automatically set my insulin to carb ratio {myFunction()}. It just wont show up. The code is...

<HTML>
<head>
    <meta name="viewport" content="initial-scale=2, user-scalable=no">
    <meta name= apple-mobile-web-app-capable content= yes />
    <meta name= apple-mobile-web-app-status-bar-style content= black />
    <link rel="apple-touch-icon" href="https://dl.dropboxusercontent.com/u/11681462/untitled%20folder/Icon/Icon-SD-Iphone.png" />
    <link rel="apple-touch-icon" sizes= 114x114 href="https://dl.dropboxusercontent.com/u/11681462/untitled%20folder/Icon/Icon-HD-Iphone.png" />
    <link rel="apple-touch-icon" sizes= 72x72 href="https://dl.dropboxusercontent.com/u/11681462/untitled%20folder/Icon/Icon-SD-Ipad.png" />
    <link rel="apple-touch-icon" sizes= 144x144 href="https://dl.dropboxusercontent.com/u/11681462/untitled%20folder/Icon/Icon-HD-Ipad.png" />
    <Title> Diabetes Cal </Title>
    <script>
        function valuechanged() {
    var a = document.getElementById('textA').value;
    var b = document.getElementById('textB').value;
    var t = document.getElementById('textX').value;
    var y = document.getElementById('textY').value;
    var z = document.getElementById('textZ').value;
    var dec = t * 0.1;
    var x = parseFloat(dec).toFixed(1);
    if (+x - +y < 0) {
        var c = 0;
    }
         else
        {
            c = +x - +y;
        } 
       if (c < 0) {
        var l = 0;
       }
        else
        {
        var l = c / z;
        }
        var d = parseFloat(l).toFixed(2);
        if (a / b < 0)
        {
        var u = 0;
        }
        else
        {
        var u = a / b;
        }
        var e = parseFloat(u).toFixed(2);
        document.getElementById('labelS').innerHTML = e;
        document.getElementById('labelG').innerHTML = d;
        document.getElementById('labelX').innerHTML = +x;
        document.getElementById('labelA').innerHTML = a;
        document.getElementById('labelJ').innerHTML = +d + +e;
            }
        function myFunction(){  
    var TS=document.getElementById('textB')
     var date= new Date();
     var time = date.getHours();
    if (time<10)
         {
          CR = "8";
          }
        else if (time<16)
         {
          CR = "10";
          }
        else if (time<20)
         {
          CR = "5";
          }
        else
         {
         CR = "8";
          }
    TS.value= CR
    }
        </script>
    </head>
    <body BGColor=orange onload="valuechanged();myFunction();" > <center><br> Bolus Wizard <br>
         <div>
            Bg (without Decimal)<br> 
        <input type="text" pattern=\d* id="textX" min="25" max="333" value="25" onchange="valuechanged();" /><br><label ID="labelX">-----</label> mmol/L<br>

        </div><hr width=45% color=black size=0.5>
        <div>
            Carbs<br> <input type="text" pattern=\d* ID="textA" max=300 value="0" onchange="valuechanged();" /><br>
        </div><hr width=45% color=black size=0.5>
        <div>
            Carb Ratio<br><input type="text" pattern=\d* ID="textB" value="" onchange="valuechanged();" /> 
        </div>
        <div>
            Target<br><input type="text" pattern=\d* ID="textY" value="6" onchange="valuechanged();" />
        </div>
    <div>
            Correction Factor<br><input type="text" pattern=\d* ID="textZ" value="2" onchange="valuechanged();" />
        </div>
    <div>
            Food<br>  <label ID="labelS">  ---</label> Units
        </div>
    <p>
    <div>
            Correction<br>  <label ID="labelG">  ---</label> Units
        </div>
    <p>
        <div>
            You Need...<br>  <label ID="labelJ">  ---</label> Units
        </div>
    <p>
    </center>
    </body>
</HTML> 

I was hoping someone could debug my problem.t is confusing. The formula is (Blood Sugar{x}-Target{y} -:- (Correction Factor{z})+ (carbs{a} -:- Carb ratio{b}) it was working before I added the decimal placement

var dec = t * 0.1; var x = parseFloat(dec).toFixed(1);

also if anyone could teach me how to create a settings for the

TextZ, TextY and TextB

area on a different page that would be awesome. PM me(if you can on here)

Thanks SOO much

Upvotes: 0

Views: 163

Answers (2)

HMR
HMR

Reputation: 39290

I don't know if the calculations are right but your code contains a line:

document.getElementById('labelA').innerHTML = a;

Problem is that your document doesn't contain an element with the id of labelA so getElementById returns null then you try to get innerHTML of null wich causes an Error.

If you comment out that line it seems to be doing something.

One tip: If you're using firefox, chrome or opera you can press F12 and check out the console for javascript errors. In your code you can use console.log to log from your code like:

console.log("is variable a what I think it is:",a);

Upvotes: 1

Afsar
Afsar

Reputation: 3124

you need to change this line:

  document.getElementById('labelA').innerHTML = a;

to this :

  document.getElementById('textA').innerHTML = a;

Upvotes: 0

Related Questions