Created
June 25, 2013 15:56
-
-
Save edegula/5859647 to your computer and use it in GitHub Desktop.
Manually searching for texts in JS
This file contains hidden or 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
var text = "thisis is sisi sis iskallakksdj lakdjf asdf hark \ | |
the cannons hear them Elias alksdfjalksdjkjaskdjflk kajsdfkj \ | |
Erwin kajsdklfjasdfjlkj asdf Erwin sakaksl askdfjaskdfj Erwin", | |
myName = "Erwin", | |
hits = []; | |
for ( var i = 0; i < text.length; i++ ) { | |
if ( text[i] === myName[0] ) { | |
var count = 0; | |
var index = hits.length - 1; | |
for ( var j = i, k = 0; j < myName.length + i; j++, k++ ) { | |
if ( text[j] === myName[k]) { | |
hits.push(text[j]); | |
count++; | |
} else { | |
hits.splice(index,count); | |
} | |
} | |
} | |
} | |
if ( hits.length === 0 ) { | |
console.log("Your name wasn't found!"); | |
} else { | |
console.log(hits); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment