user4860805
user4860805

Reputation:

function always returns true

I have a function that is comparing two arrays. The blacklist array is all lowercase already.

The function always returns true and I don't see why.

function flagProfanity(words) {
    var found = false;
    $.each(words, function(key, word){
        if ($.inArray(word.toLowerCase, blacklist) == -1){
            found = true;
        }
    });
    return found;   
}

EDIT, these are alerted straight from words, and a censored version of blacklist.

k,just,wondering,how,this,mod,thing,works

swear, profanity, bad, word, more, profanity

Upvotes: 1

Views: 122

Answers (2)

FuzzyTree
FuzzyTree

Reputation: 32392

inArray returns -1 if the needle is not found, so you'll want to set found to true if inArray returns a value that's greater than -1.

Also, as Bergi pointed out in the comments you want to call toLowerCase(), which is a function, and not toLowerCase, which is a property.

if ($.inArray(word.toLowerCase(), blacklist) > -1){
  found = true;
}

Upvotes: 0

rafaelc
rafaelc

Reputation: 59274

That will only return false if, and only if, the arrays are completely equal. If there is at least one element (word) that is not in the array, found will be set to true and that is why it always return true.

Upvotes: 1

Related Questions