Function check: why gives IE an error and firefox not???

Discussion in 'JavaScript' started by 123GoToAndPlay, May 12, 2007.

  1. #1
    This gives an error in IE

    
    function x() {
    
    length = getPathLength();
    
    }
    
    
    function getPathLength() {
    //return the length of the path
    	var num = polyPoints.length;
    
    	if(num > 1){
    	var ret = 0;
    	for(var i = 0; i < (num-1); ++i){
    		  ret += polyPoints[i].distanceFrom(polyPoints[i+1]);
    	}
    		ret = ret.toFixed(3)/1000;
    		return ret;
    	}
      return NaN;
    }
    
    
    Code (markup):
    I know length = getPathLength(); gives me the error in IE, because if i deleted that part it doesn't show an error.

    Any tips??
     
    123GoToAndPlay, May 12, 2007 IP
  2. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #2
    What exactly is "polyPoints"? You're getting an error cause "polyPoints" is undefined...
     
    SoKickIt, May 12, 2007 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    no, that's properly not it.

    polyPoints is a global var.

    Like i said if i delete
    
    length = getPathLength(); 
    
    Code (markup):
    from the function i get no errors

    Any other thoughts?
     
    123GoToAndPlay, May 13, 2007 IP
  4. gandaliter

    gandaliter Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Can you fill in the rest of the script including the (assuming html) that calls it. At a guess I'd say that the reason you don't get an error when you remove that line of code is because the function is only called from that line of code. Generally speaking firefox is much more forgiving that IE.
     
    gandaliter, May 13, 2007 IP
  5. Aztral

    Aztral Well-Known Member

    Messages:
    344
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    125
    #5
    Try i++ instead of ++i

    I believe in the very first iteration of your loop i will be 1 not 0,
    so that in your last loop i = num (because you are pre-incrementing i before entering loop.

    you evaluate i < (num - 1) then immediatly increment i.

    i++ says do the loop then increment me.
    ++i says increment me then to loop

    Anyway, this code looks familiar.
    Aren't you also going to want an if statement in the loop

    like

    if( i == (num - 1))
    ret += polyPoints.distanceFrom(polyPoints[0]);
    else
    ret += polyPoints.distanceFrom(polyPoints[i+1]);

    that will compute the total boundry length if your points are stored in order (say...clockwise direction)
     
    Aztral, May 17, 2007 IP