Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help debugging function

Status
Not open for further replies.

electricphp

Programmer
Feb 20, 2008
71
US
I have the following function which is not outputting anything when I'm calling it:

Code:
<script language=JAVASCRIPT type="TEXT/JAVASCRIPT">
		<!-- Hide script from older browsers

function percentiles(sex,age,value) {

  if (sex == 2) {
    var percentiles_array = girls_bmi;   
 } else {
    var percentiles_array = boys_bmi;   
 }

   for (var i = 0; i < percentiles_array.length; i ++) {
	     data = percentiles_array[i];
		 details = data.split("|");
		 details_age = details[0];
		 if (age == details_age) {
		      var L = details[1];
		      var M = details[2];
		      var S = details[3];
		      break;
		 }		 
   }

  if(L==0) {
    Z = log(value/M)/S
  } else {
    Z = (Math.pow((value/M),L)-1)/(L*S)
  }
   
  P=1-1/Math.sqrt(2*3.14159265)*Math.exp(-Math.pow(Math.abs(Z),2)/2)*(0.4361836*(1/(1+0.33267*Math.abs(Z)))-0.1201676*Math.pow((1/(1+0.33267*Math.abs(Z))),2)+0.937298*Math.pow((1/(1+0.33267*Math.abs(Z))),3))
    if(Z>0) {
      centile = P*100
    } else {
      centile = 100-P*100
    }

    
  return Math.round(centile*10)/10
}
		
		// End hiding script -->
		</script>

The part that's supposed to output the percentile is this:
Code:
<script language=JAVASCRIPT type="TEXT/JAVASCRIPT">  
percentile = percentiles(1, 5, 19);
percentile = parseInt(percentile);
document.write("percentile: " + percentile);
</script>

but it's not outputing anything

any help would be appreciated

 
- log should be Math.log unless you've defined your own log function.

- Do you see any JS errors?

- Are girls_bmi and boys_bmi defined anywhere?

- What do girls_bmi and boys_bmi hold? What format is the data stored in?

Those aside, I'd change this:

Code:
percentile = parseInt(percentile);

to this:

Code:
percentile = parseInt(percentile[!], 10[/!]);

to stop any potential errors, and this:

Code:
<script language=JAVASCRIPT type="TEXT/JAVASCRIPT">

to this:

Code:
<script type="text/javascript">

unless you need to support very old browsers.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
you guys are right, i need to know the values for the boys and girls array in order for the script to work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top