0

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');

    }
  }
}
0

3 Answers 3

2

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');

    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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..

1 Comment

Thanks Abhinsit. Super appreciated!
0

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');
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.