Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-11-2012, 04:47 PM   PM User | #1
mfandel
New to the CF scene

 
Join Date: Dec 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
mfandel is an unknown quantity at this point
help with output formating

I am not a Javascript developer, though I was able to use some opensource code and make most of the changes that I needed for my needs. I am just having problems with adjusting some of the output.

I have a script that calculates percentages based on a given starting number.
Which seems to work fine. A demo can be viewed here:
http://athleticimprovementcenter.com...alculator.html

I need to modify the low end and high end to not be a calculation, but to say "lower" for "0" and on the high end I get "NaN" but need it to be the word "greater"

Problem two is that the caclulations are based on the previous fields results for all zones and need to overide for certain zones. so for example zone 3, low end is the high end from zone 2. I need to be able to have the low end of for certain zones be based on a different percentage and not the low end of the previous zone. Again for only certain zones.

Code:
<SCRIPT language="javascript">
<!----
window.onError = null; 
var justloaded=true;
var expireDate = new Date; 
var dc = document.cookie
var nZones=15
if(dc!="")var aCookies=document.cookie.split(";");
function readcookie(){
	var f = document.frm_Heart_Rate_Calculator;
	var mhr="";
	if(dc!=""){
		for(var i=0; i<aCookies.length; i++){
			mhr = eval(aCookies[i].split("=")[1]);
			f.txt_Maxmum_HeartRate.value=mhr;
		}
	}
}
function writecookie(){
	var f=eval(parseInt(document.frm_Heart_Rate_Calculator.txt_Maxmum_HeartRate.value));
	expireDate.setYear(expireDate.getYear()+1)
	if(f>0 && !isNaN(f)){
		document.cookie = "mhr=" + f + "; expires=" + expireDate.toGMTString();
	}
}
function deletecookie(){
	if(document.cookie!=""){
		if(confirm("Do you want to remove the cookies to?")){
			expireDate.setYear(expireDate.getYear()-1)
			document.cookie = "mhr= ; expires=" + expireDate.toGMTString();
		}
	}
}
function browsertype(){
	var n=navigator.appName
	if(n == 'Microsoft Internet Explorer')	return "IE";
	else if(n == 'Netscape')return "NC";
	else return "unknown";
}
function browserver(v){
	var iVer;
	var strVer=navigator.appVersion.toLowerCase()
	if(browsertype()=="IE"){
		var ipos=eval(strVer.indexOf('msie '))
		iVer= parseFloat( strVer.substring(ipos+5,strVer.length) ) 
	}else{
		iVer=parseFloat(strVer);
	}
	return (iVer>=v)?true:false;
}
function PreLoadImage(imgsrc){
	if(browserver(4)){
		var head1= new Image();
		head1.src=imgsrc;
	}
return head1;
}
function ShowButton(imagename,objectsrc){
	if(browserver(4)) document.images[imagename].src=eval(objectsrc+".src");
};
function ShowQuad(loc1,loc2,loc3,loc4, objectsrc1,objectsrc2,objectsrc3,objectsrc4){
	ShowButton(loc1, objectsrc1);
	ShowButton(loc2, objectsrc2);
	ShowButton(loc3, objectsrc3);
	ShowButton(loc4, objectsrc4);
};




function clearform(){
	var f=document.frm_Heart_Rate_Calculator;
	f.txtage.value="";
	f.txt_Maxmum_HeartRate.value="";
	for(i=0;i<nZones;i++) f.txt[i].value="";
}
function makepos(n){
	return (n<0)? -n: n;
}
function posnum(n){
	if(isNaN(n) || n==0){
		if(!justloaded)	alert("Invalid entry");
		justloaded=false;
		return false;
	}
return true;
}
var a_Heart_Rate_Factors = new Array(0,0.81,0.89,0.93,0.99,1.02,1.06)
function calculate_heart_zones(nLower,nUpper){
	var sout=""
	sout+="<br>" + (a_Heart_Rate_Factors[nLower]*100) + "-" + (a_Heart_Rate_Factors[nUpper]*100) + "%"
	document.write(sout)
}
function calculate_heart_rate_levels(){
	var s= " - ";
	var b=" BPM";
	var f=document.frm_Heart_Rate_Calculator;
	var max=makepos(parseInt(f.txt_Maxmum_HeartRate.value));
	if(posnum(max)){
		f.txt_Maxmum_HeartRate.value=max;
		f.txt[0].value="Low End"+parseInt(max*a_Heart_Rate_Factors[0])+b;
		for(i=1;i<nZones;i++)	f.txt[i].value=parseInt(max*a_Heart_Rate_Factors[i-1])+s+parseInt(max*a_Heart_Rate_Factors[i])+b;
	}
	else clearform();
}


// --->
</SCRIPT>
</HEAD>
<BODY>
<CENTER>


<table border=0>
<CENTER>

<img src="http://www.athleticimprovementcenter.com/aicblog/files/images/hrcalc.jpg">
</CENTER>
<br>
<br>



<CENTER>
<TABLE BORDER=0 CELLPADDING="5" width=958px>

<TR>
<TD ALIGN=CENTER>
	
	<FORM NAME="frm_Heart_Rate_Calculator">
	
	<TABLE BORDER=1 cellspacing=5>


	<TR><TD ALIGN=RIGHT>
	Lactate Threshold (HR)
	</TD>
	<TD width=200px>
	<INPUT TYPE="text" NAME="txt_Maxmum_HeartRate" VALUE="" MAXLENGTH=3 SIZE=10 onChange="keyhandler(this,'max')" onkeyup="keyhandler(this,'max')" onFocus="this.select();this.focus();">
	</TD>	
	<TD><INPUT TYPE="button" NAME="cmdlevels" VALUE="Calculate Training Zones for LTHR" onClick="calculate_heart_rate_levels();writecookie();">
	</TD>
	</TR>
	
	
	
	<TR>
	<TD valign=bottom align=right><b><u>Zone</u> &nbsp;&nbsp;</b></TD>
	<TD valign=bottom  align=center><b><u>Range BPM</u></b></TD>
	<td  valign=bottom align=left><b> &nbsp;&nbsp;<u>Purpose</u></b></td>
	</TR>
	

	
	<TR><TD valign=top ALIGN=RIGHT>
	<font color=darkblue><b>Zone 1</b>
		<script>
		<!--
		calculate_heart_zones(0,1)
		//--->
	</script>
</font>
	</TD>
	<TD ALIGN=left valign=top>
	<INPUT TYPE="hidden" NAME="txt" VALUE="" SIZE=20><INPUT TYPE="text" NAME="txt" VALUE="Enter LTHR Above" SIZE=20>
	</TD>
	<TD valign=top ALIGN=left><font color=darkblue>Less than 81% of LTHR
</font>
	</TD>
	</TR>
	<TR>
	<TD valign=top ALIGN=RIGHT>
	<font color=darkblue><b>Zone 2</b>
	<script>
		<!--
		calculate_heart_zones(1,2)
		//--->
	</script>
	</font>
	</TD>
	<TD ALIGN=left valign=top>
	<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
	</TD>
	<TD valign=top ALIGN=left><font color=darkblue>81% to 89% of LTHR
</font>
	</TD>
	</TR>
<TR>
	<TD valign=top ALIGN=RIGHT>
	<font color=darkgreen><b>Zone 3</b>
	<script>
		<!--
		calculate_heart_zones(2,3)
		//--->
	</script>
	</font>
	</font>
	</TD>
	<TD ALIGN=left valign=top>
	<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
	</TD>
	<TD valign=top ALIGN=left><font color=darkgreen>90% to 93% of LTHR
</font>
	</TD>
	</TR>
<TR>
	<TD valign=top ALIGN=RIGHT>
	<font color=darkgreen><b>Zone 4</b>
	<script>
		<!--
		calculate_heart_zones(3,4)
		//--->
	</script>
	</font>
	</TD>
	<TD ALIGN=left valign=top>
	<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
	</TD>
	<TD ALIGN=left valign=top><font color=darkgreen>94% to 99% of LTHR</font></TD>
	</TR>
	<tr>
	<TD valign=top ALIGN=RIGHT>
	<font color=red><b>Zone 5a</b>
	<script>
		<!--
		calculate_heart_zones(4,5)
		//--->
	</script>
	</font>
	</TD>
	<TD ALIGN=left valign=top>
	<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
	</TD>
	<TD ALIGN=left valign=top><font color=red>100% to 102% of LTHR

</font>
	</TD>
	</tr>
<tr>
	<TD valign=top ALIGN=RIGHT>
	<font color=red><b>Zone 5b</b>
	<script>
		<!--
		calculate_heart_zones(5,6)
		//--->
	</script>
	</font>
	</TD>
	<TD ALIGN=left valign=top>
	<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
	</TD>
	<TD ALIGN=left valign=top><font color=red>103% to 106% of LTHR

</font>
	</TD>
	</tr>
	<tr>
	<TD valign=top ALIGN=RIGHT>
	<font color=red><b>Zone 5c</b>
	<script>
		<!--
		calculate_heart_zones(6,6)
		//--->
	</script>
	</font>
	</TD>
	<TD ALIGN=left valign=top>
	<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
	</TD>
	<TD ALIGN=left valign=top><font color=red>More than 106% of LTHR
</font>
	</TD>
	</tr>
	</TABLE>
	</FORM>
</TD>
</TR>
mfandel is offline   Reply With Quote
Old 12-11-2012, 09:16 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Why in the world do you use document.write() in that code???

Anyway, it looks trivial:

Code:
        for( i=1; i<nZones - 1; i++ )
        {
            f.txt[i].value = parseInt(max*a_Heart_Rate_Factors[i-1])+s+parseInt(max*a_Heart_Rate_Factors[i])+b;
        }
        f.txt[i].value = parseInt(max*a_Heart_Rate_Factors[i-1])+s+"Greater"+b;
You already special cased "Lower" so just special case "Greater".
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-11-2012, 09:23 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
And there is really no reason to be doing browser detection. None of the code I see there would have problems with any browser from MSIE 5 (1999) on forward.

And your cookies don't seem to work.

Oh...I see why...it's because your calculate_heart_rate() function gets an error.

But in any case, why do you need to save ANYTHING except the Lactose Threshold in the cookie? Everything else can be (and is) calculated from that.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-11-2012, 09:26 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
And you have some horribly illegal HTML!

Just this part, alone:
Code:
<table border=0>
<CENTER>
<img src="http://www.athleticimprovementcenter.com/aicblog/files/images/hrcalc.jpg">
</CENTER>
<br>
<br>
You can't contain anything inside of a <table> except table elements: <tbody>, <tr>, etc.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-11-2012, 11:42 PM   PM User | #5
mfandel
New to the CF scene

 
Join Date: Dec 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
mfandel is an unknown quantity at this point
I really appreciate the response. As i mentioned I know very little about javascript and was trying to make something I found work for my needs.

There is possibly some code in there from things I stripped out from the front end that I was not using. The original code had a calculation based on other variables to create the zones that I did not need.

I am a bit confused since I am clueless. I added the code to specify the "greater" but doesn't seem to have changed anything.

I loaded a copy with your suggested code change and compared the two with no change in output. Revised version here: http://athleticimprovementcenter.com...lculator2.html

Thanks again!
mfandel is offline   Reply With Quote
Old 12-12-2012, 07:05 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
okay...very late here. Will try to look at it tomorrow.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-12-2012, 11:10 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Before I go further: It looks like you really wanted tables for both Bike Zones and Run Zones but then couldn't figure out how to put in both.

DO you want BOTH?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-13-2012, 03:09 PM   PM User | #8
mfandel
New to the CF scene

 
Join Date: Dec 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
mfandel is an unknown quantity at this point
I need two calculators... my plan was to get the first one working and then duplicate for the second. I don't want both to be available in the same window/page. I need to place one in a section for runners and the other in a seperate page for cycling.

I really appreciate you taking the time on this!

I think I found a way to trick the existing code to deal with the previous percentage used to calculate the second zone or getting close. I can put in a hidden field for before lets say zone 3. It then uses the previous zone highend, but hidden then I can calculate a unique lower end more or less for the zone. Probably not the best method, but seems to be working.

If the works the big issue or one I can't work around is change the lower end or first zone low end from "0" to some sort of text and then in the very last zone 5c how to insert text of "or greater" for the NaN default.
mfandel is offline   Reply With Quote
Old 12-18-2012, 05:30 PM   PM User | #9
mfandel
New to the CF scene

 
Join Date: Dec 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
mfandel is an unknown quantity at this point
Only One Last Issue

I was able to get the calculations to work with the hidden fields, so I am good there.

So the only remaining thing is how to change the output in the table after the calculation for bottom end and top end. Currnetly the Zone 1 gives you 0-(calculated HR) and I need to change the "0" to the text "less than"

For the highend zone, zone 5c the test NaN appears in two places left column and in the calculation (calculated HR)-NaN. I need the NaN to output text "Greater Than".

Link to working page:
http://www.athleticimprovementcenter...lculator4.html


Code:
<SCRIPT language="javascript">
<!----
window.onError = null; 
var justloaded=true;
var expireDate = new Date; 
var dc = document.cookie
var nZones=15
if(dc!="")var aCookies=document.cookie.split(";");
function readcookie(){
	var f = document.frm_Heart_Rate_Calculator;
	var mhr="";
	if(dc!=""){
		for(var i=0; i<aCookies.length; i++){
			mhr = eval(aCookies[i].split("=")[1]);
			f.txt_Maxmum_HeartRate.value=mhr;
		}
	}
}
function writecookie(){
	var f=eval(parseInt(document.frm_Heart_Rate_Calculator.txt_Maxmum_HeartRate.value));
	expireDate.setYear(expireDate.getYear()+1)
	if(f>0 && !isNaN(f)){
		document.cookie = "mhr=" + f + "; expires=" + expireDate.toGMTString();
	}
}
function deletecookie(){
	if(document.cookie!=""){
		if(confirm("Do you want to remove the cookies to?")){
			expireDate.setYear(expireDate.getYear()-1)
			document.cookie = "mhr= ; expires=" + expireDate.toGMTString();
		}
	}
}
function browsertype(){
	var n=navigator.appName
	if(n == 'Microsoft Internet Explorer')	return "IE";
	else if(n == 'Netscape')return "NC";
	else return "unknown";
}
function browserver(v){
	var iVer;
	var strVer=navigator.appVersion.toLowerCase()
	if(browsertype()=="IE"){
		var ipos=eval(strVer.indexOf('msie '))
		iVer= parseFloat( strVer.substring(ipos+5,strVer.length) ) 
	}else{
		iVer=parseFloat(strVer);
	}
	return (iVer>=v)?true:false;
}
function PreLoadImage(imgsrc){
	if(browserver(4)){
		var head1= new Image();
		head1.src=imgsrc;
	}
return head1;
}
function ShowButton(imagename,objectsrc){
	if(browserver(4)) document.images[imagename].src=eval(objectsrc+".src");
};
function ShowQuad(loc1,loc2,loc3,loc4, objectsrc1,objectsrc2,objectsrc3,objectsrc4){
	ShowButton(loc1, objectsrc1);
	ShowButton(loc2, objectsrc2);
	ShowButton(loc3, objectsrc3);
	ShowButton(loc4, objectsrc4);
};




function clearform(){
	var f=document.frm_Heart_Rate_Calculator;
	f.txtage.value="";
	f.txt_Maxmum_HeartRate.value="";
	for(i=0;i<nZones;i++) f.txt[i].value="";
}
function makepos(n){
	return (n<0)? -n: n;
}
function posnum(n){
	if(isNaN(n) || n==0){
		if(!justloaded)	alert("Invalid entry");
		justloaded=false;
		return false;
	}
return true;
}
var a_Heart_Rate_Factors = new Array(0,0.81,0.89,0.90,0.93,0.94,0.99,1,1.02,1.03,1.06)
function calculate_heart_zones(nLower,nUpper){
	var sout=""
	sout+="<br>" + (a_Heart_Rate_Factors[nLower]*100) + "-" + (a_Heart_Rate_Factors[nUpper]*100) + "%"
	document.write(sout)
}
function calculate_heart_rate_levels(){
	var s= " - ";
	var b=" BPM";
	var f=document.frm_Heart_Rate_Calculator;
	var max=makepos(parseInt(f.txt_Maxmum_HeartRate.value));
	if(posnum(max)){
		f.txt_Maxmum_HeartRate.value=max;
		f.txt[0].value="Low End"+parseInt(max*a_Heart_Rate_Factors[0])+b;
		        for( i=1; i<nZones - 1; i++ )
        {
            f.txt[i].value = parseInt(max*a_Heart_Rate_Factors[i-1])+s+parseInt(max*a_Heart_Rate_Factors[i])+b;
        }
        f.txt[i].value = parseInt(max*a_Heart_Rate_Factors[i-1])+s+"Greater"+b;
	}
	else clearform();
}


// --->
</SCRIPT>
mfandel is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:50 PM.


Advertisement
Log in to turn off these ads.