www.webdeveloper.com
Results 1 to 3 of 3

Thread: java calculations

  1. #1
    Join Date
    Mar 2011
    Posts
    5

    java calculations

    I need help on a code that wont calculate for a bmi calculator for some reason it just wont show results when I click on calculate. Also I want the reverse bmi calculator to grab height and weight from the first calculator, I will put all codes pasted below. Thank you in advance for your help.

    ---bmi calculator---

    <form><table border="0" width="200" align="center"><tbody><tr><td colspan="2"

    align="center"><label for="d1"><input type="radio" id="d1" name="d"

    value="1,1">cm/kg</label><label for="d2"><input type="radio" id="d2" name="d"

    value="2.54,2.2">in/lb</label></td></tr><tr><td align="right"><label

    for="h">Height:</label></td><td align="left"><input type="text" id="h" name="h"

    size="6"></td></tr><tr><td align="right"><label for="w">Weight:</label></td><td

    align="left"><input type="text" id="w" name="w" size="6"></td></tr><tr><td colspan="2"

    align="center"><input type="button" value="Calculate" onclick="calc(this.form);return

    false;"></td></tr><tr><td align="right"><label for="f">Your BMI:</label></td><td

    align="left"><input type="text" id="f" name="f" size="6"

    readonly="readonly"></td></tr></tbody></table></form>



    ---javascript---

    function valButton(btn) {var cnt = -1;for (var i=btn.length-1; i > -1; i--) { if (btn[i].checked) {cnt = i; i = -1;}}if (cnt > -1) return btn[cnt].value;else return null;}function stripBlanks(fld) {var result = "";var c = 0;for (i=0; i < fld.length; i++) {if (fld.charAt(i) != " " || c > 0) {result += fld.charAt(i);if (fld.charAt(i) != " ") c = result.length;}}return result.substr(0,c);}function calc(thisform) {var d = valButton(thisform.d);if (d == null) {alert("You must choose centimetres/kg or inches/lb");return false;}cv = d.split(',');;var h = stripBlanks(thisform.h.value);if (h == '') {alert("You must enter your height");thisform.h.focus();return false;}if (h != Number(h) || (h = Number(h*cv[0]/100)) < 1 || h > 2.5) {alert("Invalid height entered");thisform.h.focus();return false;}var w = stripBlanks(thisform.w.value);if (w == '') {alert("You must enter your weight");thisform.w.focus();return false;}if (w != Number(w) || (w = Number(w/cv[1])) < 25 || w > 250) {alert("Invalid weight entered");thisform.w.focus();return false;} thisform.f.value = Math.round(w / (h*h)*100)/100;}


    ---reverse bmi calculator---

    ***this one is the html only i dont have a calc yet for this***

    <form action="" method="post" name="BMI_input" class="main_text" id="BMI_input">
    <p style="text-align: center;">Your
    Height:<b>
    <input id="htf2" type="text" name="htf" value="<a href="bmicalc.js"" size="3" maxlength="1" />
    </b>(feet)<b>
    <input id="FormsEditField3" type="text" name="hti" value="" size="3"
    maxlength="2" />
    </b>(inches)</p>
    <p style="text-align: center;"><font face="Arial, Helvetica, sans-serif">Target
    BMI:<b>&nbsp;
    <input id="FormsEditField4" type="text" name="wt" value="24" size="3" maxlength="2" />
    <br />
    </b></font></p>
    <p style="text-align: center;"> <font face="Arial, Helvetica, sans-serif">
    <input type="button" name="FormsButton1" value="Calculate Target Weight" id="FormsButton1" onclick="self.compute()" />
    </font></p>
    <p style="text-align: center;"><font face="Arial, Helvetica, sans-serif">This
    is your Target Weight: <br />
    <input id="FormsEditField5" type="text" name="bmi" value="" size="4" maxlength="5" />
    &nbsp; </font></p>
    </form>

  2. #2
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    Quote Originally Posted by vavi2s View Post
    I need help on a code that wont calculate for a bmi calculator for some reason it just wont show results when I click on calculate. Also I want the reverse bmi calculator to grab height and weight from the first calculator, I will put all codes pasted below. Thank you in advance for your help.
    --snip---
    ---javascript---

    function valButton(btn) {var cnt = -1;for (var i=btn.length-1; i > -1; i--) { if (btn[i].checked) {cnt = i; i = -1;}}if (cnt > -1) return btn[cnt].value;else return null;}function stripBlanks(fld) {var result = "";var c = 0;for (i=0; i < fld.length; i++) {if (fld.charAt(i) != " " || c > 0) {result += fld.charAt(i);if (fld.charAt(i) != " ") c = result.length;}}return result.substr(0,c);}function calc(thisform) {var d = valButton(thisform.d);
    ....
    Properly formatted code is a big help when debugging, one statement per line and statements under control of if, else, for, while, etc. are indented. Comments don't hurt either, especially when asking for other eyes to check out your code.
    Code:
    function stripBlanks(fld) {
      var result = "";
      var c = 0;
      for (i=0; i < fld.length; i++) {
        if (fld.charAt(i) != " " || c > 0) {
          result += fld.charAt(i);
          if (fld.charAt(i) != " ")
            c = result.length;    }
      }
      return result.substr(0,c);
    }

  3. #3
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    Buried in the javascript was the calc function that a button in bmi form starts, but there is no compute function the reverse bmi tries to start.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles