Skip to content

Instantly share code, notes, and snippets.

@majornorth
Created May 9, 2014 22:28
Show Gist options
  • Save majornorth/93f834fb2698e1da4bb7 to your computer and use it in GitHub Desktop.
Save majornorth/93f834fb2698e1da4bb7 to your computer and use it in GitHub Desktop.
Codecademy:JavaScript::Search Text for Your Name (5/7)
/*jshint multistr:true */
var text = "Stewart containing some text for Stewart written and developed by Stewart";
var myName = "Stewart";
var hits = [];
/* assign the value of 0 to i and continue to the loop while i is less than the length of the variable 'text', and add 1 to i after executing the code block */
for (i = 0; i < text.length + 1; i++) {
/* if the array value of the 'text' variable is equal to the 0 point of the myName variable */
if(text[i] === myName[0]) {
/* then assign the value of i to j and continue the loop while j is less than the length of the myName variable plus the value of i */
for(var j = i; j < (myName.length + i); j++) {
/* if the array value of the variable 'text' is equal to the array value of j minus i for the 'myName' variable, push the value of j on to the hits array */
if(text[j] === myName[j-i]) {
hits.push(text[j]);
}
}
}
}
if(hits.length === 0){
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}
Copy link

ghost commented Nov 19, 2014

Can somebody please explain to me why in " if(text[j] === myName[j-i]) {
hits.push(text[j]);
}" they put j-i and just explain the whole hits.push thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment