![]() |
Moving text to an array - question. Easy for you master coders!
Code:
text = "Blah blah blah blah blah blah Tim \ |
What this code does:
It scans the string in the variable "text" letter by letter, and as soon as it finds a "T" (i.e. any T), it will move the T and the following two letters to a result array named "hits" ... it will do that even if the "T" is not followed by "im". Example: text="This is Tim and Thomas and Theodore" In the end the hits array will be ["T", "h", "i", "T", "i", "m", "T", "h", "o", "T", "h", "e"] with a length of 12. This is certainly not what you wanted Try this instead Code:
var hits = text.match(/Tim/g).length; |
Hey dude. Thanks very much for the reply. I am aware this is not perfect code, it's just a lesson on code academy. I'm also aware it's bad code and will yield terrible results, but as it stands if it picks from those letters it will display "t" "i" "m", but I have no clue why I have to put myName.length + i to get those letters. I don't know why I can't just put myName.length, and it's confusing the hell out of me!
edit: Should I just not worry about it and move on? It's really a weight on my shoulders right now |
If you go through the code step by step you will immediately see
Given: text="My name is Tim and not Thomas" myName = "Tim" The outer loop with var "i" will count up without doing anything until it reaches the first "T" character, which is at position i==11 (counting starts at 0) The inner loop with var "j" will now start at position i==11 and count up to myName.length+i-1 which is 3+11-1 = 13 (length of "Tim" plus i, minus 1 because of the <) and every character from text between position 11 and (including) position 13 will be copied into the result array "hits", so it will be ["T", "i", "m"] Then the outer loop with var "i" will continue to count up without doing anything until it reaches the next "T" character, which is at position i==23 The inner loop with var "j" will now start at position i==23 and count up to myName.length+i-1 which is 3+23-1 = 25 (length of "Tim" plus i, minus 1 because of the <) and every character from text between position 23 and (including) position 25 will be copied into the result array "hits", so it will be ["T", "i", "m", "T", "h", "o"] Then the outer loop with var "i" will continue to count up without doing anything until it reaches the next "T" character ... or the end of the string. hits.length == 6, so the "else" part of the condition will be executed, outputting the full "hits" array THE END |
Not using a regular expression:-
Code:
<script type = "text/javascript">text(i) is undefined. Use text.charAt(i) instead. While this finds Tim but not Thomas, it will also find Timothy and Timpson so not very reliable. The regex should read Code:
var hits = text.match(/\bTim\b/g).length; // Tim as a whole word only - not Timothy or Timpson |
Quote:
|
Thanks devnull! Thanked and repped in response!
It's a big help to see it broken down like that! |
Quote:
And thanks a lot for your help too mate, but I'm not looking for better code, I just wanted to know why I had to add + i to the for! Thanks anyway! |
Quote:
|
Oh good ... you're right
I thought I was stupid when I answered like this in another thread, because when I tried it, it just worked :-) |
Quote:
|
FWIW, this is kind of a nonsensical way to do this, no matter how you look at it.
More sensiible, in my opinion: Code:
var text="My name is Tim and not Thomas"If you preferred to collect the *letters* of each: Code:
if ( word.charAt(0) == "T" )Code:
if ( word.charAt(0) == "T" ) |
Thanks for your time guys! You are all stars to write this stuff out !
|
| All times are GMT +1. The time now is 01:41 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.