Created
June 22, 2015 10:44
-
-
Save lucylow/32d42fe4bc3be933248e to your computer and use it in GitHub Desktop.
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
// 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