Lets say I have a string of fruit names
var string = "cherries,oranges,limes"
and an array of red fruit
var array = ["tomatoes", "cherries", "raspberries"]
in javascript if I want to find if the string has any red fruit, I can do
for(var i=0; i<array.length; i+=1){
if(string.search(array[i])!=-1){
return string.search(array[i]);
}
How would this compare with the following?
var string_array= string.split(',');
for(var i=0; i<array.length; i+=1){
for(var j=0; j<string_array.length; j+=1){
if(string_array[j]==array[i]){
return string_array[j];
}
}
}
return -1;