0

I have the simple following code :

var S = require('string');

function matchBlacklist(inputString) {
    var blacklist = ["facebook", "wikipedia", "search.ch", "local.ch"];
    var found = false;
    for (var i = 0; i < blacklist.length; i++) {

        if (S(inputString).contains(blacklist[i]) > -1) {
            found = true;
        }
    }
    return (found);
}

matchBlacklist("www.facebook.com/this_is_a_test"); // returns true
matchBlacklist("www.example.com/this_is_a_test"); // returns true

But it always returns true; as it should return false for the second case

3
  • 1
    Perhaps a loop structure issue, perhaps instead of found = true; and then continuing with the for loop, why not just return true; to exit the loop? Commented Mar 15, 2018 at 14:21
  • i wonder why using ` require('string');` when string prototype already have what you need Commented Mar 15, 2018 at 14:26
  • I'd also use the built-in URL package if I wanted to separate various aspects of the URL to search on (host/path?query etc) Commented Mar 15, 2018 at 15:21

3 Answers 3

4

You should test for if(string.includes(substring)){ ... } and not if(string.includes(substring) > -1 ){ ... }

but here's a more elegant one-liner :

const blacklist = ["facebook", "wikipedia", "search.ch", "local.ch"];

const matchBlacklist = inputString => blacklist.some(word => inputString.includes(word))

console.log( matchBlacklist("www.facebook.com/this_is_a_test") ); // returns true
console.log( matchBlacklist("www.example.com/this_is_a_test") ); // returns false

Sign up to request clarification or add additional context in comments.

Comments

2

A better approach using the find method of arrays, which return the element if the expression evaluates true else undefined, then return a boolean based on the result of the find

Find method MDN

Includes method MDN

function matchBlacklist(inputString) {
    var blacklist = ["facebook", "wikipedia", "search.ch", "local.ch"];
    
    return !!blacklist.find(b => inputString.includes(b))
}

console.log(matchBlacklist("www.facebook.com/this_is_a_test"));
console.log(matchBlacklist("www.example.com/this_is_a_test"));

Comments

0

You can use "indexOf" instead "contains" method. "contains" method will return boolean value, so you cannot compare the boolean value with a number. "indexOf" will return a index number of a string, so you can do the comparison here.

var S = require('string');

function matchBlacklist(inputString) {
  var blacklist = ["facebook", "wikipedia", "search.ch", "local.ch"];
  var found = false;
  for (var i = 0; i < blacklist.length; i++) {

    if (S(inputString).indexOf(blacklist[i]) > -1) {
        found = true;
    }
  }
  return (found);

}

matchBlacklist("www.facebook.com/this_is_a_test"); // returns true
matchBlacklist("www.example.com/this_is_a_test"); // returns truetes

1 Comment

the main problem with this method is that if the blacklist is 1000 words long, it will go through every single word every time, even if the first word is a match, wasting 999 CPU cycles. Not to mention it will call the S() function, whatever that is, 999 useless times, because its output is always the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.