I have a script that I am working on that I am a little lost on.
I grab a user input (using jQuery) and then loop through an array and see if a phrase from the array exists in the user input. If it is found I am popping up an alert box telling them its found. But for some reason its not working.
Code:
submit = false;
for(i in keyphrases_array) {
if(english_text.indexOf(keyphrases_array[i]) != -1) {
if(trans_text.indexOf(keyphrases_array[i]) != -1) {
submit = false;
}
} else {
submit = true;
}
}
keyphrases_array is a global array that is set in another function.
Example:
If in my array I have the phrase “Hello World” and my script finds Hello World in the english_text variable it then checks the user submitted text (trans_text). If the phrase is not found in trans_text then the script should return false.
First, you shouldn’t use
for/into iterate over an array. Use a plainforloop.The problem is likely that the value of
submitis being overwritten by the next iteration in the loop. If that’s the end of the function, you should return immediately.or if there needs to be more processing, you can just break the loop:
EDIT: Based on your comment, it seems that you want to
return falseif the text is not found intrans_text. This would meant that the test should use==instead of!=.