Skip to content

Commit b83af51

Browse files
first occurence of string
1 parent f54ba5c commit b83af51

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

FirstOccurrenceString.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//haystack = "sadbutsad", needle = "sad"
2+
3+
var strStr = function (haystack, needle) {
4+
let returnIndex = -1;
5+
for (let i = 0; i < haystack.length; i++) {
6+
if (haystack[i] === needle[0]) {
7+
let match = haystack.substring(i, needle.length + i);
8+
if (match === needle) {
9+
returnIndex = i;
10+
break;
11+
}
12+
}
13+
}
14+
return returnIndex;
15+
};
16+
17+
console.log(strStr("good", "ood"));

0 commit comments

Comments
 (0)