barrylachapelle
barrylachapelle

Reputation: 987

Comparing Arrays with nested loops

I am trying to compare values of two arrays against each other. If a match is found - do something - else do this.

I put together a fiddle with my code at http://jsfiddle.net/ZvmHx/1/

If you uncomment the second the alert on line 14 you'll see what is wrong. I can't seem to prevent the second alert from firing.

Thanks!

var getkeywords = ["John","Frank","Sarah"]; 
var captionarray = ["Jim","Joe","Lee","Steve","John","Michelle","Brad"];    

for (k = 0; k < getkeywords.length; k++) {
    for (l = 0; l < captionarray.length; l++) {

    if(getkeywords[k] == captionarray[l]){

        alert('Found > ' + getkeywords[k] + ':filter image');

    }else{

        //alert('not found > ' + getkeywords[k] + ':filter image');

    }
  }
}

Upvotes: 0

Views: 3734

Answers (3)

Alex Fitiskin
Alex Fitiskin

Reputation: 775

Before alerting the result you must to compare value with all items in second array I updated your jsfiddle — try in out http://jsfiddle.net/ZvmHx/5/

var getkeywords = ["John","Frank","Sarah"]; 
var captionarray = ["Jim","Joe","Lee","Steve","John","Michelle","Brad"];    

imagecode = '';
for (k = 0; k < getkeywords.length; k++) {
    var isExists = false;
    for (l = 0; l < captionarray.length; l++) {
        if (getkeywords[k] == captionarray[l]){                     
            isExists = true;
            break;
        }
    }

    if (isExists) {
        alert('Found > ' + getkeywords[k] + ':filter image');
    } else {
        alert('not found > ' + getkeywords[k] + ':filter image');
    }
}

Upvotes: 0

abhinsit
abhinsit

Reputation: 3272

I have created a new fiddle:-

http://jsfiddle.net/WZGyy/

var getkeywords = ["John","Frank","Sarah"]; 
var captionarray = ["Jim","Joe","Lee","Steve","John","Michelle","Brad"];    

imagecode = '';
var found=0;
for (k = 0; k < getkeywords.length; k++) 
{
    for (l = 0; l < captionarray.length; l++) 
    {

        if(getkeywords[k] == captionarray[l])
        {


            found=1;
            break;

        }
    }

    if(found==1)
    {
        alert('Found > ' + getkeywords[k] + ':filter image');
        found=0;  


    }
    else
    {
        alert('not found > ' + getkeywords[k] + ':filter image');

    }
}

Hope that helps..

Upvotes: 0

freejosh
freejosh

Reputation: 11383

The if/else is being tested for every iteration of your inner loop. I think what you're after is testing if you have a match after the inner loop has run. Something like:

var getkeywords = ["John","Frank","Sarah"]; 
var captionarray = ["Jim","Joe","Lee","Steve","John","Michelle","Brad"];    
var matchFound;

for (k = 0; k < getkeywords.length; k++) {
    matchFound = false;

    for (l = 0; l < captionarray.length; l++) {
        if (getkeywords[k] == captionarray[l]){
            matchFound = true;
            break;
        }
    }

    if(matchFound){

        alert('Found > ' + getkeywords[k] + ':filter image');

    }else{

        alert('not found > ' + getkeywords[k] + ':filter image');

    }
}

Upvotes: 2

Related Questions