Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   Help with Calculator (http://www.programmingforums.org/showthread.php?t=13288)

MiKuS Jun 5th, 2007 11:01 PM

Help with Calculator
 
I currently am making a calculator (to learn javascript better)

it's a karvonen heart rate calculator which calculates what your target heart rate BPM should be for a percentage workout, the formula is as follows:

(maximum heart rate = maximum achievable heart rate)
(minimum heart rate = heart rate measured just after waking up)

maximum heart rate - minimum heart rate * workout percentage + minimum heart rate = target bpm

ok, now i can get the formula kinda working.. the poblem i have is with this code:

targetHeartRateLow=Math.floor((maxHeartRate-minHeartRate)*percentZoneMin/100)+minHeartRate;

instead of adding minHeartRate it joins it like two strings like 4499 instead of showing 143 (44+99). Below is the code i'm using

:

function calc(form){
var maxHeartRate;
var minHeartRate;
var percentZoneMin;
var percentZoneMax;
var targetHeartRateLow;
var targetHeartRateHigh;

minHeartRate=form.rest_hr.value;
maxHeartRate=form.max_hr.value;
percentZoneMin=form.percent_zone1.value;
percentZoneMax=form.percent_zone2.value;

targetHeartRateLow=Math.floor((maxHeartRate-minHeartRate)*percentZoneMin/100)+minHeartRate;
targetHeartRateHigh=Math.floor((maxHeartRate-minHeartRate)*percentZoneMax/100)+minHeartRate;
form.target_hr1.value=targetHeartRateLow;
form.target_hr2.value=targetHeartRateHigh;

}


Infinite Recursion Jun 6th, 2007 8:09 AM

look into using the parseInt function.
(http://www.w3schools.com/jsref/jsref_parseInt.asp)

MiKuS Jul 9th, 2007 12:44 AM

it's been a while since i last posted (been busy) but i got it in the end, here is the code i used.

:

  1. //Work Out Heart Rate Min - Max
  2. function calc(form){
  3. var maxHeartRate;
  4. var minHeartRate;
  5. var percentZoneMin;
  6. var percentZoneMax;
  7. var targetHeartRateLow;
  8. var targetHeartRateHigh;
  9. //Get form values
  10. minHeartRate=parseInt(form.rest_hr.value,10);
  11. maxHeartRate=parseInt(form.max_hr.value,10);
  12. percentZoneMin=form.percent_zone1.value;
  13. percentZoneMax=form.percent_zone2.value;
  14. //Run our calculation
  15. targetHeartRateLow=Math.floor((maxHeartRate-minHeartRate)*percentZoneMin/100)+minHeartRate;
  16. targetHeartRateHigh=Math.floor((maxHeartRate-minHeartRate)*percentZoneMax/100)+minHeartRate;
  17. //update our form
  18. form.target_hr1.value=targetHeartRateLow;
  19. form.target_hr2.value=targetHeartRateHigh;
  20.  
  21. }


Thanks for the help.

Grich Sep 23rd, 2007 6:46 PM

I know it's over, but you could also use the Number() function as well as the parseInt() function. ;)
It brings back a float / double.


All times are GMT -5. The time now is 12:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC