Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong with this code? I think I messed up in the communication between HTML and Javascript
    primarykey
    data
    text
    <p>I'm a total newbie to programming. I've been doing exercises on Codecademy for JavaScript for a little while, but there's obviously huge amounts I still don't know/understand. I've written a few things in those exercises, but nothing that anyone has or would actually use. I was planning on keeping it that way for a little longer, but today at work a chance came up to do a little thing and I wanted to challenge myself so I decided to try it.</p> <p>The idea is to create a form where people can enter their basic information and have it set them a daily calorie amount that conforms to their weight loss goals. It's based around the Basal Metabolic Rate, the Harris Benedict Equation, and a little twist to fit my company's particular program.</p> <p>I was able to find the Javascript on Dreamingincode for a basic BMR calculator which I then modified to make the adjustments we need. Somewhere in that process, something has gone wrong. When I save the file as a .html file and then open it in the browser, the form appears and you can fill everything out, but when you click the button, it just refreshes the screen. </p> <p>I know this is stupid to all of you that actually know what you're doing, but I really want to make this work. If any of you feel like being heroic, please look at what I have and tell me where I screwed up.</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Daily Calorie Goal&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript" language="javascript"&gt; function Calculate() { var gender = document.getElementById("gender").value; var weight = document.getElementById("weight").value; var height = document.getElementById("height").value; var age = document.getElementById("age").value; var goal = document.getElementById("goal").value; if(gender=="male") { val1 = 6.23 * weight; val2 = 12.7 * height; val3 = 6.8 * age; dailyDeficit = (goal * 3500) / 90; result = 66 + val1 + val2 - val3; cals = result * 1.55; calMax = cals - dailyDeficit; } else if (gender=="female") { val1 = 6.23 * weight; val2 = 4.7 * height; val3 = 4.7 * age; dailyDeficit = (goal * 3500) / 90; result = 655 + val1 + val2 - val3; cals = result * 1.55; calMax = cals - dailyDeficit; } document.write ('This is your Daily Calorie Goal. To achieve your goal, just consume fewer than this number of calories every day:&lt;b&gt; ' + calMax.toFixed(2) + '&lt;/b&gt;&lt;br&gt;'); } &lt;/script&gt; &lt;form action="#"&gt; Gender : &lt;select id="gender"&gt;&lt;option value="male"&gt;Male&lt;/option&gt;&lt;option value="female"&gt;Female&lt;/option&gt;&lt;/select&gt;&lt;br /&gt; Weight (lbs.) : &lt;input type="text" id="weight" /&gt;&lt;br /&gt; Height (inches): &lt;input type="text" id="height" /&gt;&lt;br /&gt; Age : &lt;input type="text" id="age" /&gt;&lt;br /&gt; Goal : &lt;select id="Goal"&gt;&lt;option value=5&gt;Lose 5 Pounds&lt;/option&gt;&lt;option value=10&gt;Lose 10 Pounds&lt;/option&gt;&lt;option value=15&gt;Lose 15 Pounds&lt;/option&gt;&lt;option value=20&gt;Lose 20 Pounds&lt;/option&gt;&lt;option value=25&gt;Lose 25 Pounds&lt;/option&gt;&lt;/select&gt;&lt;br /&gt; &lt;/fieldset&gt; &lt;input type="submit" value="Get My Daily Calorie Goal" onclick="Calculate()" /&gt; &lt;/form&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload