Skip to content

Commit

Permalink
String Search
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsosborne committed Sep 7, 2017
1 parent b7cfc8f commit 8596df3
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Chapter 4 - Strings and Associative Arrays/stringSearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// String.search(val) - search string for the given val (another string). Return the index position of the first match found(or -1 if not found).

String.prototype.search = function(val){
var str = this.toString();
var strArr = str.split("");
var temp;
var count = 0;
for (var x = 0; x < strArr.length; x++){
if(strArr[x] == val[count]){
count++;
}
if(strArr[x] == val[0]){
console.log(x);
temp = x;
}
if(count == val.length){
return temp;
}
}
}

console.log("My trip to Antartica".search("trip"));

0 comments on commit 8596df3

Please sign in to comment.