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!

Matching names from 1 array in another array

Status
Not open for further replies.

timbrown2009

Programmer
Mar 27, 2009
1
GB
Hi everyone,
I'm new to this forum and just wondering if someone could give me a hand quickly. I have this piece of code that I want to be able to search for names in an array and if it finds them then it matches them.

e.g. The code below should find 2 matches.But for some reason I can't get it to work :-(

Hope someone can help. Thanks
===================================================
Code:
name = [Simon, Jim,Mark,Ryan];
stored_names = [Jim,Simon,James,Simon,Tim];

var match_checker = 0;   
		
	for (var count_index = 0; count_index < name.length; count_index = count_index ++) 
	{	
		if (name[count_index] == stored_names)
		{
			match_checker = match_checker + 1;    
	}
	document.write (match_checker);  //display numb of matches
}	
	}
 
Hi

I skip the comments. There would be many, because your code is plain crap. Sorry.
Code:
name = ['Simon', 'Jim','Mark','Ryan'];
stored_names = ['Jim','Simon','James','Simon','Tim'];

var match_checker = 0;   
        
for (var count_index = 0; count_index < name.length; count_index ++) {
  for (var another_index = 0; another_index <stored_names.length; another_index ++) {
    if (name[count_index] == stored_names[another_index]) {
      match_checker = match_checker + 1;
      break;   
    }
  }
}    
document.write (match_checker);

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top