Created
June 1, 2017 01:44
-
-
Save supercodingninja/34d4279c1705787f144474ce5baa9ed8 to your computer and use it in GitHub Desktop.
null created by supercodingninja - https://repl.it/I0iG/0
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
/*jshint multistr:true */ | |
var text = "Once upon a time, a man named, 'Frederick,' travelled from a distant land.\ Frederick did not like the lot in life, that he was living; so, he joined his nation's military force. He journeyed through distant lands, across the globe he lived on.\ Frederick was still not happy; and he began to cry out to GOD, in Heaven; and GOD heard and answered Frederick."; | |
var myName = "Frederick"; | |
var hits = []; | |
// Look for "F" in the text | |
for(var i = 0; i < text.length; i++) { | |
if (text[i] === "F") { | |
// If we find it, add characters up to | |
// the length of my name to the array | |
for(var j = i; j < (myName.length + i); j++) { | |
hits.push(text[j]); | |
} | |
} | |
} | |
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
How to search for name, within a block of text.