SOURCE

var haystack = "leetcode", needle = "ee";
var strStr = function(haystack, needle) {
    var lenP = haystack.length, lenC = needle.length;
    if (lenP == 0 && lenC == 0) {  // 允许一个为空进行搜索
        return 0;
    }
    if (lenP < lenC) {
        return -1;
    }
    for (var i = 0; i < lenP - lenC + 1; i++) {  // 注意i的范围
        if (haystack.substr(i,lenC) == needle) {
            return i;
        }
    }
    return -1;
}; 

console.log(strStr(haystack,needle));
console 命令行工具 X clear

                    
>
console