-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7cfc8f
commit 8596df3
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
Chapter 4 - Strings and Associative Arrays/stringSearch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |