Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't this work? I think it's a conversion issue
    primarykey
    data
    text
    <p>I'm a scripting newbie who's been doing the JavaScript exercises at Codecademy for a little while now and a chance came up at work to challenge myself by building a really simple app (HTML and JavaScript)that allows someone to calculate the number of calories they can consume on a daily basis while meeting their weight loss objectives.</p> <p>I've got it almost there. I had a working version (thanks to some help) that asked in the form for people to give their height in inches. The last change I wanted to make was to add the ability for them to enter their height in standard format (feet and inches) and have the app convert it in the equation. That's where the trouble started. I added a second field and renamed the variables where needed to create the conversion, but what happened is strange.</p> <p>It calculates properly when something is entered into the "feet" field, but if you also add something into the "inches" field on the form, it returns results that are more than 4 times as high as they should be.</p> <p>I tried moving the conversion down into the gender functions, but it made no difference. I haven't changed anything else about the code that was formerly working, so I think the issue has to be in the inches variable or in the inches form element or in the height variable where they are combined. The latter seems most likely to me, but I can't seem to find the problem.</p> <p>It's probably obvious to those of you who know what you're doing so if you could take a few seconds, you'd be helping someone make the first real-world use app of their lives.</p> <p>Side Note: When I open it in my browser(Chrome), the first time I fill it out, the the answer flashes and disappears. After that, each time it pulls up the answer correctly on the screen. I'm not sure if this is some mistake I've made or a browser issue or what.</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Daily Calorie Max&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 inches = document.getElementById("inches").value; var height = (document.getElementById("feet").value * 12) + inches; 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.getElementById("answer").textContent = 'Your Daily Calorie Max is: ' + calMax.toFixed(0); } &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 : &lt;input type="text" id="weight" /&gt;lbs.&lt;br /&gt; Height : &lt;input type="text" id="feet" /&gt;ft. &lt;input type="text" id="inches" /&gt;in.&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="Give me my Daily Calorie Max!" onclick="Calculate()" /&gt; &lt;/form&gt; &lt;div id="answer"&gt;&lt;/div&gt; &lt;/body&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