Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Dec 2012
    Location
    Ingerlund
    Posts
    66
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Moving text to an array - question. Easy for you master coders!

    Code:
    text = "Blah blah blah blah blah blah Tim \
    blah blah blah Tim blah blah Tim blah blah \
    blah blah blah blah blah Tim";
    
    var myName = "Tim";
    var hits = [];
    
    for (var i = 0; i < text.length; i++){
    	if (text[i]== "T"){
    		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);
    }
    Hi! This is basically a small project to search the text above and to move the letters of a name to an array using for loops. I've constructed the code in a specific way pertinent to the exercise I was doing on CodeAcademy, but I just don't understand this part - (myName.length + i). I don't know why I have to put + i there, why can't I just tell the code to stop running when the letters that have been moved to the array are EQUAL to myname? I hope that makes sense.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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;
    console.log(hits);

  • #3
    New Coder
    Join Date
    Dec 2012
    Location
    Ingerlund
    Posts
    66
    Thanks
    8
    Thanked 4 Times in 4 Posts
    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

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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

  • Users who have thanked devnull69 for this post:

    Entity_ (12-18-2012)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Not using a regular expression:-

    Code:
    <script type = "text/javascript">
    
    text = "Blah blah blah blah blah blah Tim \
    blah Terry blah blah Tim blah Thomas blah Tim blah blah \
    blah blah blah blah blah Tim";
    
    var myName = "Tim";
    var hits = [];
    var n = "";
    for (var i = 0; i < text.length; i++){
    if (text.charAt(i)== "T") {
    for (var j = i; j < (myName.length+i); j++) {
    n += text.charAt(j);
    }
    if (n == myName) {
    hits.push(n);
    }
    n = "";
    }
    }
    
    
    if (hits.length === 0){
    alert("Your name wasn't found!");
    } 
    else {
    alert(hits);
    alert (hits.length);
    }
    
    </script>
    myName.length + i means the next 3 characters following the current position which is index i. As devnull69 has explained.
    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
    alert (hits);
    Last edited by Philip M; 12-18-2012 at 07:19 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    text(i) is undefined. Use text.charAt(i) instead.
    But text[i] is fine, and he used this notation

  • #7
    New Coder
    Join Date
    Dec 2012
    Location
    Ingerlund
    Posts
    66
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Thanks devnull! Thanked and repped in response!

    It's a big help to see it broken down like that!

  • #8
    New Coder
    Join Date
    Dec 2012
    Location
    Ingerlund
    Posts
    66
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Philip M View Post
    Not using a regular expression:-

    Code:
    <script type = "text/javascript">
    
    text = "Blah blah blah blah blah blah Tim \
    blah Terry blah blah Tim blah Thomas blah Tim blah blah \
    blah blah blah blah blah Tim";
    
    var myName = "Tim";
    var hits = [];
    var n = "";
    for (var i = 0; i < text.length; i++){
    if (text.charAt(i)== "T") {
    for (var j = i; j < (myName.length+i); j++) {
    n += text.charAt(j);
    }
    if (n == myName) {
    hits.push(n);
    }
    n = "";
    }
    }
    
    
    if (hits.length === 0){
    alert("Your name wasn't found!");
    } 
    else {
    alert(hits);
    alert (hits.length);
    }
    
    </script>
    myName.length + i means the next 3 characters following the current position which is index i. As devnull69 has explained.
    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
    alert (hits);


    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!

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,628
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Quote Originally Posted by devnull69 View Post
    But text[i] is fine, and he used this notation
    That notation has only recently been introduced into JavaScript and so only works with some browsers. If you want it to work for all browsers you need to use charAt() instead.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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 :-)

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by devnull69 View Post
    But text[i] is fine, and he used this notation
    Sorry, typo for text[i]. But as felgall says, not supported by some browsers.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    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"
    var words = text.split(/\b/g); // the \b there says "break on word boundaries"
    var found = [ ];
    for ( var w = 0; w < words.length; ++w )
    {
        var word = words[w];
        if ( word.charAt(0) == "T" )
        {
             found.push( word );
        }
    }
    At the end of this code, found will have two elements: "Tim" and "Thomas".

    If you preferred to collect the *letters* of each:
    Code:
        if ( word.charAt(0) == "T" )
        {
             for ( var c = 0; c < word.length; ++c )
             {
                 found.push( word.charAt(c) );
             }
        }
    If you really do want only 3 letters per found word (why??), then:
    Code:
        if ( word.charAt(0) == "T" )
        {
             for ( var c = 0; c < word.length && c < 3; ++c )
             {
                 found.push( word.charAt(c) );
             }
        }
    It makes much more sense to break the string into "words". You now only have to check the first letter of each word, not every character in the string.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New Coder
    Join Date
    Dec 2012
    Location
    Ingerlund
    Posts
    66
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Thanks for your time guys! You are all stars to write this stuff out !


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •