Last active
December 21, 2015 16:49
-
-
Save stevencombs/6336432 to your computer and use it in GitHub Desktop.
Javascript code to search through a text string for specific text.
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
// 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