Searching a string with text search

Posted on

Problem

say that:

var stringToTest = "Hello i am walking"

And

var searchText = "Hello walking"

I have the following javascript function for filtering.

var searchSplit = searchText.split(" ");
var num_split = searchSplit.length;
var correctTested = 0;
searchSplit.forEach(function (testString) {
    if (stringToTest.indexOf(testString) >= 0) {
        correctTested++;
    }
});

//Decides if the value are close enough
if (correctTested > 0) {
    var percentageCorrect = correctTested / num_split * 100;
    return percentageCorrect >= 50;
}
else {
    return false;
}

I have never done anything like this and i would like if you would rate or even come with suggestions on how to improve the above code.

Solution

My suggestions are mostly focused on making the code as concise and clear as possible.

  1. Simplify the return statement.

    if (correctTested > 0) {
        var percentageCorrect = correctTested / num_split * 100;
        return percentageCorrect >= 50;
    } else {
        return false; 
    }
    

    can be reduced to

    return correctTested >= num_split * 0.5;
    

    This eliminates both the condition as well as concern about div by 0.

  2. Since you do not need a post-increment, choose pre-increment.
    ++correctTested or correctTested += 1 rather than correctTested++.

  3. Choose clear names.

    searchSplit might be clearer as searchTokens, correctTested as matchedTokens, num_split as tokenCount.

  4. Consistent names.
    searchCase is camel case while num_split is snake case.

  5. You have not articulated the functional spec, but consider whether the search should be case-insensitive. Also consider whether partial word matches are in fact acceptable or whether you require full token matches.

For ease of testing and for the ability to add different decision criteria you could consider decomposing match counting and the decision criteria into two separate functions.

Leave a Reply

Your email address will not be published. Required fields are marked *