In summary I am trying to build a swear filter where I have a static array called “badWords” which holds many profane words.
My script then lifts all text values from a form into another array called “fieldValues”.
It then iterates through the fieldValues and should compare the string values with the whole array of “badWords” and if found trigger a simple response for now.
The code is not producing any errors but it is not producing expected outputs either, I was hoping you could give me insight into where I went wrong with it.
Code
// get all the inputs from "editor_form"
var $inputs = $('#editor_form :input');
var fieldValues = new Array();
var i = 0;
//for each field place it in array
$inputs.each(function()
{
fieldValues[i] = $(this).val();
i++;
});
var swearWord;
for (i = 0; i < fieldValues.length; i++)
{
if (fieldValues[i] == function()
{
//this bit i think is wrong, i think it only does one loop in this level then jumps back up
for (x = 0; x < badWords.length; x++)
{
return badWords[x];
}
})
//warning message
{
window.alert("Bad word found");
}
}
Thanks
First, you can fix your existing code like this :
But your bad words form, logically, a set, not an array. You should use an object as map to find them in a fast way.