0

I have an array of characters I am testing another array against. The array of strings length can dynamically change every time the app is ran. Here is the code I have, but right now it is just going through the first index of the array. How can I have it go through all the length of the array toTest

code:

var testAgainst = ['and','+',',','&'];
var toTest = ['This is just a test +','This is just a test and','This is just a test ,','This is just a test &','This is just a test'];

for(var i = 0; i < testAgainst.length; i++){

    if (toTest[0].indexOf(testAgainst[i]) > -1){
        console.log('match: ' + testAgainst[i]);
    }else{
        console.log('no match');
    }

}

JSFIDDLE: https://jsfiddle.net/bmpgsxb2/2/

Chipe
  • 4,641
  • 10
  • 36
  • 64

1 Answers1

1

All you need to do is add a for-loop to go through each element in the toTest array like so:

for(var i = 0; i < testAgainst.length; i++){
    for(var j = 0; j < toTest.length; j++){
        if (toTest[j].indexOf(testAgainst[i]) > -1){
            console.log('match: ' + testAgainst[i]);
        }else{
            console.log('no match');
        }
    }
}

Fiddle

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54