Skip to content

Instantly share code, notes, and snippets.

@stevencombs
Last active December 21, 2015 16:49
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 stevencombs/6336432 to your computer and use it in GitHub Desktop.
Save stevencombs/6336432 to your computer and use it in GitHub Desktop.
Javascript code to search through a text string for specific text.
// Javascript Getting Started with Programming
// A Codecademy Javascript (Search Text For Name) assignment
// Dr. Steven B. Combs, coding novice
var text = "blah blah blah blah Steven \
blah blah blah blah Steven blah";
var myName = "Steven";
var hits = [];
for (var i = 0; i < text.length; i++){
if (text[i] == "S"){
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);
}
// Alternative: Modify code to use the variable myName rather than
// "S" to register name and verify each letter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment