Results 1 to 3 of 3

Thread: Comparing Two Arrays, printing matches

  1. #1
    Join Date
    Mar 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Comparing Two Arrays, printing matches

    Greetings,

    I'm attempting to compare two arrays and print all of the results into a <div>.

    In this case, I'd like to compare numArray with stringSplit and have the resulting matches (which in the case I'm printing is numArray[0], numArray[1], and numArray[3]) and have those matches print from nameArray. So in the case I'm printing, I want the <div id=captainContent> to print nameArray[0], nameArray[1], and nameArray[3].

    Code is not working. Anyone see any issues?

    Thanks,

    --thesprucegoose

    Code:
    var testString = "10-20-40";
    var element = document.getElementById("captainContent");
    var numArray = new Array();
    numArray[0] = "10";
    numArray[1] = "20";
    numArray[2] = "30";
    numArray[3] = "40";
    numArray[4] = "50";
    
    var nameArray = new Array();
    nameArray[0] = "Ten";
    nameArray[1] = "Twenty";
    nameArray[2] = "Thirty";
    nameArray[3] = "Forty";
    nameArray[4] = "Fifty";
    
    
    
    var stringSplit = testString.split("-");
    
    for (i = 0; i < stringSplit.length; i++) {
    	for (j = 0; j < numArray.length; i++)  {
    		if (stringSplit[i] == numArray[j]) {
    			element.innerHTML += nameArray[j] + '<br />';					   
    		}
    	}
    	
    	
    }

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The code has some other basic issues, but if that's the general approach you want to take, here:

    Code:
    for (i = 0; i < stringSplit.length; i++) {
    	for (j = 0; j < numArray.length; i++)  {
    		if (stringSplit[i] == numArray[j]) {
    			element.innerHTML += nameArray[j] + '<br />';					   
    		}
    	}
    	
    	
    }
    That i should be j.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh no!

    Now I feel silly. Thank you so much!

Bookmarks

Posting Permissions

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