brute force search javascript

Solutions on MaxInterview for brute force search javascript by the best coders in the world

showing results for - "brute force search javascript"
Luciana
22 Jan 2021
1function bruteForceSubstringSearch(text, pattern) {
2    const lengthOfText = text.length;
3    const lengthOfPattern = pattern.length;
4    for (let i = 0; i < lengthOfText; i++) {
5        let j;
6        for (j = 0; j < lengthOfPattern; j++) {
7            if (text.charAt(i + j) !== pattern.charAt(j)) {
8                   break;
9            }
10        }
11        if (j === lengthOfPattern) return i;
12    }
13    return lengthOfText;
14}
15
16bruteForceSubstringSearch('this is a test', 'test');