-2

Currently doing a BMR calculator for practice but I can't seem to get the function to work. Trying to use the male bmr formula if the male radio button is checked, but no alert appears.

<script>
    if(gender==male){
        function bmr(weight, height, age) {
            var malebmr = (66 + (13.7 * weight) + (5 * height) - (6.8 * age));
            return malebmr;
        }
    else {
        function bmr(weight, height, age) {
            var femalebmr = (655 + (9.5 * weight) + (1.8 * height) - (4.7 * age));
            return femalebmr;
        }
    }
    }

    function calculatebmr() {       
        alert(bmr(parseFloat(document.bmiform.weight.value),
        parseFloat(document.bmiform.height.value),
        parseFloat(document.bmiform.age.value)));
    }

</script>    

<fieldset data-role="controlgroup" data-type="horizontal">
    <legend>Gender</legend>
    <label for="male">Male</label>
    <input type="radio" name="gender" value="male" id="male" checked>
    <label for="female">Female</label>
    <input type="radio" name="gender" value="female" id="female">
</fieldset>
<input type="button" value="Calculate"
onclick="calculatebmr()">
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
nicholas
  • 1
  • 3
  • 1
    where do you call `calculatebmr`? – Domysee Nov 07 '15 at 06:48
  • I recommend working through some basic JavaScript and web programming tutorials. There are too many distinct, fundamental problems with that code for SO's format. – T.J. Crowder Nov 07 '15 at 06:50
  • @nicholas I just formatted the code. You can see now clearly that you have a parenthesis mismatch at the first if. – Domysee Nov 07 '15 at 06:53
  • You need for one to have one function that for example could look like this: `function bmr(weight, height, age) { if (document.getElementById("male").checked) return (66 + (13.7 * weight) + (5 * height) - (6.8 * age)); else return (655 + (9.5 * weight) + (1.8 * height) - (4.7 * age)); }` – mplungjan Nov 07 '15 at 06:53

1 Answers1

0

There are many problems with the code, so I suggest you to read some tutorials about javascript. Also the code you show doesn't contain the bmiform, but I think the form is in your code.

Problem 1

You have a parenthesis mismatch. (Thanks Domysee)

Problem 2

You don't need to have two bmr() functions. You must just test inside the function which gender is selected. (Thanks mplungjan)

Problem 3

You can't just test if (gender==male) for two reasons:

  1. The gender is undefined
  2. The male is undefined

You must use:

 if (document.getElementById("male").checked) {

Remember what I said in Problem 2

(Thanks mplungjan)

Correct code

I think you will learn a lot of more, if you try to make it work yourself with the help of above descriptions. But if you just want a working code, here you go.

Html:

Your current code works, if you have the bmiform with inputs weight, height and age.

Javascript:

function bmr(weight, height, age) {
    if (document.getElementById("male").checked) {
        return (66 + (13.7 * weight) + (5 * height) - (6.8 * age)); /* Male */
    }
    else {
        return (655 + (9.5 * weight) + (1.8 * height) - (4.7 * age)); /* Female */
    }
}

function calculatebmr() {       
    alert(bmr(parseFloat(document.bmiform.weight.value),
    parseFloat(document.bmiform.height.value),
    parseFloat(document.bmiform.age.value)));
}


Question for somebody who knows more than me :)

Is it allowed in HTML5 to use

document.bmiform.weight.value

Or must we use

document.getElementById('weight').value

or something else?



Ps. Sorry for my english, I'm from Finland :)

Community
  • 1
  • 1
TuomasK
  • 482
  • 3
  • 11
  • According to this post, the collections are obsolete: http://stackoverflow.com/questions/2435525/best-practice-access-form-elements-by-html-id-or-name-attribute - I no longer use them, but they made sense when passing the form to the function: `function calculatebmr(theForm) { var height=parseInt(theForm.height.value,10); ...` – mplungjan Nov 07 '15 at 14:09