Skip to content

Instantly share code, notes, and snippets.

@lucylow
Created June 22, 2015 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucylow/53cfcd19f0c6876953c1 to your computer and use it in GitHub Desktop.
Save lucylow/53cfcd19f0c6876953c1 to your computer and use it in GitHub Desktop.
// Program searches for a name given a long string. First by finding the first letter of the name, then adding the length of the name to an aaray and inspecting the aaray.
// Variables for long text string, name, and the aaray where answer will be stored
var text = "Hi, my name is Lucy. How are you doing?
var myName ="Lucy";
var hits= [];
// For loop searches the text by first letter of name
for(var i=0;i<text.length;i++){
if(text[i] === myName[0]){ // text[i] will be the current letter searched and myName[0] is the first letter of name
for(var j=i;j<myName.length+i;j++) // i will be the current letter searched in text, and j will be the letter in the name
{
hits.push(text[j]); // adds the current letter of the name to an aaray
}
}
}
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