Last active
February 10, 2016 23:43
-
-
Save quassy/d471bc8b87918483ff83 to your computer and use it in GitHub Desktop.
Help you understand this?
This file contains hidden or 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
text = "Kanye says: This string has the name Kenny embeded in it\n the following function should find it."; | |
var myName = "Kenny"; //String to find | |
var hits = []; // Empty array to put it into | |
var altHits = []; // Empty list for alternative hits | |
// Look for "K" in the text | |
/* | |
Loop over numbers from `0` to `text.length` | |
so you can access the single characters of `text` | |
via `text[i]` for the `i+1`th character | |
(strings are loopable like arrays / lists) | |
*/ | |
for(var i = 0; i < text.length; i++) { // No clue | |
console.log("inside 1st for-loop, current index i="+i+" & and current character text[i]="+text[i]); | |
/* | |
Check if the current character `text[i]` is equal to K, | |
if yes do further stuff | |
text[0] is 1st character (K), text[1] is 2nd character (a), etc. | |
*/ | |
if (text[i] === "K") { // I guess telling i to look for K??? | |
console.log(" inside if-condition, because character text[i]===K"); | |
// If we find it, add characters up to | |
// the length of my name to the array | |
/* | |
Start a new loop at the position of the found "K" `i` and name the running index `j`, | |
the loop runs `myName.length` times, so in this case 5 times | |
*/ | |
for(var j = i; j < (myName.length + i); j++) { // Where did j come from? | |
console.log(" inside 2nd for-loop, current index j="+j+" and current character text[j]="+text[j]); | |
/* | |
Problems: | |
1. This code also adds "Kanye" (added at top) and any list of characters starting with "K", | |
there should be a check if the following characters actually match "Kenny" | |
2. The resulting array will look like [ "K", "a", "n", "y", "e", "K", "e", "n", "n", "y" ], | |
I'd expect it should actually be ["Kanye", "Kenny"] | |
*/ | |
hits.push(text[j]); // push found text to array but where did j come from? | |
console.log(" after adding the current character to hits array, it looks like this: hits="+hits); | |
} | |
/* | |
This is my alternative to the above for loop which uses the | |
[substr method](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/substring) | |
*/ | |
if (text.substr(i, myName.length) === myName) { | |
altHits.push(text.substr(i, myName.length)); | |
} | |
} | |
} | |
if (hits.length === 0) { // if nothing was found. | |
console.log("Your name wasn't found!"); | |
} else { | |
console.log(hits); | |
} | |
if (altHits.length === 0) { // if nothing was found. | |
console.log("Your name wasn't found!"); | |
} else { | |
console.log(altHits); | |
} |
This file contains hidden or 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
inside 1st for-loop, current index i=0 & and current character text[i]=K | |
inside if-condition, because character text[i]===K | |
inside 2nd for-loop, current index j=0 and current character text[j]=K | |
after adding the current character to hits array, it looks like this: hits=K | |
inside 2nd for-loop, current index j=1 and current character text[j]=a | |
after adding the current character to hits array, it looks like this: hits=K,a | |
inside 2nd for-loop, current index j=2 and current character text[j]=n | |
after adding the current character to hits array, it looks like this: hits=K,a,n | |
inside 2nd for-loop, current index j=3 and current character text[j]=y | |
after adding the current character to hits array, it looks like this: hits=K,a,n,y | |
inside 2nd for-loop, current index j=4 and current character text[j]=e | |
after adding the current character to hits array, it looks like this: hits=K,a,n,y,e | |
inside 1st for-loop, current index i=1 & and current character text[i]=a | |
inside 1st for-loop, current index i=2 & and current character text[i]=n | |
inside 1st for-loop, current index i=3 & and current character text[i]=y | |
inside 1st for-loop, current index i=4 & and current character text[i]=e | |
inside 1st for-loop, current index i=5 & and current character text[i]= | |
inside 1st for-loop, current index i=6 & and current character text[i]=s | |
inside 1st for-loop, current index i=7 & and current character text[i]=a | |
inside 1st for-loop, current index i=8 & and current character text[i]=y | |
inside 1st for-loop, current index i=9 & and current character text[i]=s | |
inside 1st for-loop, current index i=10 & and current character text[i]=: | |
inside 1st for-loop, current index i=11 & and current character text[i]= | |
inside 1st for-loop, current index i=12 & and current character text[i]=T | |
inside 1st for-loop, current index i=13 & and current character text[i]=h | |
inside 1st for-loop, current index i=14 & and current character text[i]=i | |
inside 1st for-loop, current index i=15 & and current character text[i]=s | |
inside 1st for-loop, current index i=16 & and current character text[i]= | |
inside 1st for-loop, current index i=17 & and current character text[i]=s | |
inside 1st for-loop, current index i=18 & and current character text[i]=t | |
inside 1st for-loop, current index i=19 & and current character text[i]=r | |
inside 1st for-loop, current index i=20 & and current character text[i]=i | |
inside 1st for-loop, current index i=21 & and current character text[i]=n | |
inside 1st for-loop, current index i=22 & and current character text[i]=g | |
inside 1st for-loop, current index i=23 & and current character text[i]= | |
inside 1st for-loop, current index i=24 & and current character text[i]=h | |
inside 1st for-loop, current index i=25 & and current character text[i]=a | |
inside 1st for-loop, current index i=26 & and current character text[i]=s | |
inside 1st for-loop, current index i=27 & and current character text[i]= | |
inside 1st for-loop, current index i=28 & and current character text[i]=t | |
inside 1st for-loop, current index i=29 & and current character text[i]=h | |
inside 1st for-loop, current index i=30 & and current character text[i]=e | |
inside 1st for-loop, current index i=31 & and current character text[i]= | |
inside 1st for-loop, current index i=32 & and current character text[i]=n | |
inside 1st for-loop, current index i=33 & and current character text[i]=a | |
inside 1st for-loop, current index i=34 & and current character text[i]=m | |
inside 1st for-loop, current index i=35 & and current character text[i]=e | |
inside 1st for-loop, current index i=36 & and current character text[i]= | |
inside 1st for-loop, current index i=37 & and current character text[i]=K | |
inside if-condition, because character text[i]===K | |
inside 2nd for-loop, current index j=37 and current character text[j]=K | |
after adding the current character to hits array, it looks like this: hits=K,a,n,y,e,K | |
inside 2nd for-loop, current index j=38 and current character text[j]=e | |
after adding the current character to hits array, it looks like this: hits=K,a,n,y,e,K,e | |
inside 2nd for-loop, current index j=39 and current character text[j]=n | |
after adding the current character to hits array, it looks like this: hits=K,a,n,y,e,K,e,n | |
inside 2nd for-loop, current index j=40 and current character text[j]=n | |
after adding the current character to hits array, it looks like this: hits=K,a,n,y,e,K,e,n,n | |
inside 2nd for-loop, current index j=41 and current character text[j]=y | |
after adding the current character to hits array, it looks like this: hits=K,a,n,y,e,K,e,n,n,y | |
inside 1st for-loop, current index i=38 & and current character text[i]=e | |
inside 1st for-loop, current index i=39 & and current character text[i]=n | |
inside 1st for-loop, current index i=40 & and current character text[i]=n | |
inside 1st for-loop, current index i=41 & and current character text[i]=y | |
inside 1st for-loop, current index i=42 & and current character text[i]= | |
inside 1st for-loop, current index i=43 & and current character text[i]=e | |
inside 1st for-loop, current index i=44 & and current character text[i]=m | |
inside 1st for-loop, current index i=45 & and current character text[i]=b | |
inside 1st for-loop, current index i=46 & and current character text[i]=e | |
inside 1st for-loop, current index i=47 & and current character text[i]=d | |
inside 1st for-loop, current index i=48 & and current character text[i]=e | |
inside 1st for-loop, current index i=49 & and current character text[i]=d | |
inside 1st for-loop, current index i=50 & and current character text[i]= | |
inside 1st for-loop, current index i=51 & and current character text[i]=i | |
inside 1st for-loop, current index i=52 & and current character text[i]=n | |
inside 1st for-loop, current index i=53 & and current character text[i]= | |
inside 1st for-loop, current index i=54 & and current character text[i]=i | |
inside 1st for-loop, current index i=55 & and current character text[i]=t | |
inside 1st for-loop, current index i=56 & and current character text[i]= | |
inside 1st for-loop, current index i=57 & and current character text[i]= | |
inside 1st for-loop, current index i=58 & and current character text[i]=t | |
inside 1st for-loop, current index i=59 & and current character text[i]=h | |
inside 1st for-loop, current index i=60 & and current character text[i]=e | |
inside 1st for-loop, current index i=61 & and current character text[i]= | |
inside 1st for-loop, current index i=62 & and current character text[i]=f | |
inside 1st for-loop, current index i=63 & and current character text[i]=o | |
inside 1st for-loop, current index i=64 & and current character text[i]=l | |
inside 1st for-loop, current index i=65 & and current character text[i]=l | |
inside 1st for-loop, current index i=66 & and current character text[i]=o | |
inside 1st for-loop, current index i=67 & and current character text[i]=w | |
inside 1st for-loop, current index i=68 & and current character text[i]=i | |
inside 1st for-loop, current index i=69 & and current character text[i]=n | |
inside 1st for-loop, current index i=70 & and current character text[i]=g | |
inside 1st for-loop, current index i=71 & and current character text[i]= | |
inside 1st for-loop, current index i=72 & and current character text[i]=f | |
inside 1st for-loop, current index i=73 & and current character text[i]=u | |
inside 1st for-loop, current index i=74 & and current character text[i]=n | |
inside 1st for-loop, current index i=75 & and current character text[i]=c | |
inside 1st for-loop, current index i=76 & and current character text[i]=t | |
inside 1st for-loop, current index i=77 & and current character text[i]=i | |
inside 1st for-loop, current index i=78 & and current character text[i]=o | |
inside 1st for-loop, current index i=79 & and current character text[i]=n | |
inside 1st for-loop, current index i=80 & and current character text[i]= | |
inside 1st for-loop, current index i=81 & and current character text[i]=s | |
inside 1st for-loop, current index i=82 & and current character text[i]=h | |
inside 1st for-loop, current index i=83 & and current character text[i]=o | |
inside 1st for-loop, current index i=84 & and current character text[i]=u | |
inside 1st for-loop, current index i=85 & and current character text[i]=l | |
inside 1st for-loop, current index i=86 & and current character text[i]=d | |
inside 1st for-loop, current index i=87 & and current character text[i]= | |
inside 1st for-loop, current index i=88 & and current character text[i]=f | |
inside 1st for-loop, current index i=89 & and current character text[i]=i | |
inside 1st for-loop, current index i=90 & and current character text[i]=n | |
inside 1st for-loop, current index i=91 & and current character text[i]=d | |
inside 1st for-loop, current index i=92 & and current character text[i]= | |
inside 1st for-loop, current index i=93 & and current character text[i]=i | |
inside 1st for-loop, current index i=94 & and current character text[i]=t | |
inside 1st for-loop, current index i=95 & and current character text[i]=. | |
Array [ "K", "a", "n", "y", "e", "K", "e", "n", "n", "y" ] | |
Array [ "Kenny" ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment