Created
April 3, 2015 07:55
-
-
Save ZuraGuerra/997f21953bad0340a279 to your computer and use it in GitHub Desktop.
Find phrases in text.
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 = "banana Zura es Azur banana Zura es Azur Zura es Azur banana banana banana banana Zura es Azur"; | |
| var phrase = "Zura es Azur"; | |
| var textLow = text.toLowerCase(); | |
| var phraseLow = phrase.toLowerCase(); | |
| var hits = 0; | |
| for (var i = 0; i < textLow.length; i++) { | |
| if (textLow[i] === phraseLow.charAt(0)) { | |
| var phraseCheck = textLow[i]; | |
| for (var l = 1; l < phraseLow.length; l++) { | |
| if (textLow[i+l] === phraseLow.charAt(l)) { | |
| phraseCheck = phraseCheck + phraseLow.charAt(l); | |
| } | |
| } | |
| if (phraseCheck === phraseLow) { | |
| phraseCheck = ""; | |
| hits = hits + 1; | |
| } | |
| } | |
| } | |
| if (hits === 0) { | |
| console.log("The phrase '" + phrase + "' wasn't found! Did you misspell something?"); | |
| } | |
| else { | |
| console.log("The phrase '" + phrase + "' was found " + hits + " times."); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment