Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    simple index problem, yet my head hurts...

    I need the index of an element in a numeric array. I have tried several variations, but all of them return a undefined value. I can look at the dom and see that the value is there, but the code isn't finding it, please help.

    Code:
    Array.prototype.indexValue =function (s){
    		var index = 'It is not here';
    		for (var i=0; i<orderArray.lenght; i++){
    			if (orderArray[i] == s){
    				return i;
    			}
    		}
    	return index;
    	}
    
    function move(){
    		//get the array posstion of the page in the order array
    		var gpid = 38;
    		
    		var num = orderArray.indexValue(gpid);
    		
    		alert(num);
    }
    
    var orderArray=[28,32,38,45,65];
    For me, every time it returns, "It is not here". Help.
    TIA,
    Adam

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    I'm not yet big into JS but where in your JS is the trigger for the function? The function is called, move() - yes? but are you calling (s)? and if that isn't the fix; you've spelt length wrongly in line 3. well you've spelt it wrongly regardless

    I am looking through rum-influenced glasses so I may be wrong.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • Users who have thanked bazz for this post:

    itsafluke (06-28-2011)

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    As 'bazz' says.
    1. Fix your typo.
    2. Call the event somewhere.

    Code:
    <script type="text/javascript">
    Array.prototype.indexValue =function (s){
      var index = 'It is not here';
      for (var i=0; i<orderArray.length; i++){
        if (orderArray[i] == s) { return i; }
      }
      return index;
    }
    
    function move(){
    //get the array posstion of the page in the order array
      var gpid = 38;
      var num = orderArray.indexValue(gpid);
      alert(num);
    }
    
    var orderArray=[28,32,38,45,65]; 
    
    onload = function() {
      move();
    }
    </script>

  • Users who have thanked jmrker for this post:

    itsafluke (06-28-2011)

  • #4
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thanks

    Thanks to both of you.

    1)I didn't include the call because it is buried in a createElement event call and I thought it would not be concise to include all of that.

    2) I CAN'T BELIEVE IT WAS A TYPO! I stared at those 5 lines of code for 3 hours, and rewrote them several times! I thought being good at math would make me a good programer, but I guess I should have paid a little more attention in english class.

    3)Thanks to both of you.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Thumbs up

    Quote Originally Posted by itsafluke View Post
    Thanks to both of you.

    1)I didn't include the call because it is buried in a createElement event call and I thought it would not be concise to include all of that.

    2) I CAN'T BELIEVE IT WAS A TYPO! I stared at those 5 lines of code for 3 hours, and rewrote them several times! I thought being good at math would make me a good programer, but I guess I should have paid a little more attention in english class.

    3)Thanks to both of you.
    You're welcome, I'm sure, from both of us.
    Good Luck!


    BTW: You should mark thread as RESOLVED if you are satisfied with solutions proposes.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •