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 12 of 12
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Searching script

    I don't know what's wrong here!!! I'm trying to display only words ending in "ed", but this whole searching within scripts thing has me feeling like a flop with programming. Can anyone throw me an idea as to what I'm messing up on here?? Thanks!!


    Code:
    <html>
    <script>
       var stringInput =
       [
          "Tina is a red head.",
          "My son gets bored easily.",
          "I said I need some breaded chicken."
       ];
     
       function doCheck()
       {
          var textarea = document.getElementById("textarea");
          if (textarea)
          {
             var text = [];
             for (var index = 0; index < s.length; index++)
             {
                if (/ed\.$/i.test(s[index]))
                {
                   text.push(s[index]);
                }
             }
             textarea.value = text.join("\n");
          }
       }
    </script>
    <body onload="doCheck()">
       <textarea id="textarea" name="textarea" rows="10" cols="40"></textarea>
    </body>
    </html>

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    I haven't really worked with regular expresson before but I think that is the problem because it is only matching "ed" if it is "ed." and at the end of a string
    I think this might do it. It should at least come close:
    Code:
    regx = /ed(' '|\.|$|\.$)/i;

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Is this the part I'm supposed to change:

    Code:
    (/ed\.$/i.test(s[index])
    ?

    I'm not familiar with that code snipet, so I wasn't sure =) Thanks!

  • #4
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Yeah that's what you would change and you may want to store your regular expression in a variable that would make it reusable and easier to read

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    var text= [
    "Tina is a red head.",
    "My son gets bored easily.",
    "I said I need some breaded chicken."
    ]

    var A= text.join(' ').match(/\b[a-z]+ed\b/g);

    alert(A.length + ' words end in "ed":\n\n'+A.join(', '));

    returns:
    '4 words end in "ed":

    red, bored, need, breaded

  • #6
    New Coder
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I want it to display in the text area, so I tried the following... The text area shows, but nothing displays in it.

    Code:
    <html>
    <script>
       var text =
       [
          "Tina is a red head.",
          "My son gets bored easily.",
          "I said I need some breaded chicken."
       ];
     
       function doCheck()
       {
    
    	var checkNum = text.join(' ').match(/\b[a-z]+ed\b/g);
        
    	document.getElementById( "textarea" ).value = "The words ending in 'ed' are: " \n\n'+checkNum.join(', ');
       }
    
    </script>
    <body onload="doCheck()">
       <textarea id="textarea" name="textarea" rows="10" cols="40"></textarea>
    </body>
    </html>

  • #7
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    You were missing this single quote
    Code:
    document.getElementById( "textarea" ).value = "The words ending in 'ed' are: "+ '\n\n'+checkNum.join(', ');

  • #8
    New Coder
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I tried that and still the textbox was empty, but I tried restarting the whole thing and I came up with the below, but I'm still end up stuck with how to actually search for the words ending in "ed". The only example in our book searches for characters for the index of a character, then returns that index number. I'm just kind of shot for examples to look at and the

    Code:
    var A= text.join(' ').match(/\b[a-z]+ed\b/g);
    
    alert(A.length + ' words end in "ed":\n\n'+A.join(', '));
    contains characters I haven't worked with thus far. Out of 8 scripts this is the only one I'm not getting through. UGH!

    We're in the basics, so I'm assuming this shouldn't be AS hard as I'm making it. Thanks for the assistance, I'm trying my best on this end!!!

  • #9
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    I tested the above section and got it working. There was a missing single quote infront of your \n\n once I put that in it worked

  • #10
    New Coder
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Ok, I tried this:

    Code:
    <html>
    <script>
       var text =
       [
          "Tina is a red head.",
          "My son gets bored easily.",
          "I said I need some breaded chicken."
       ];
     
       function doCheck()
       {
    
    	var checkNum = text.join(' ').match(/\b[a-z]+ed\b/g);
        
    	document.getElementById( "textarea" ).value = "The words ending in 'ed' are: " '\n\n'+checkNum.join(', ');
       }
    
    </script>
    <body onload="doCheck()">
       <textarea id="textarea" name="textarea" rows="10" cols="40"></textarea>
    </body>
    </html>
    But the text area stays blank. And, with last night's post I SAID I was going to post what I had came up with starting from scratch, which is this:

    Code:
    <?xml version = "1.0" encoding = "utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns = "http://www.w3.org/1999/xhtml">
    	<head>
    		<title>Using IndexOf</title>
    		<script type = "text/javascript">
    			   <!--
    				var text =
            			[				[
              			"Tina is a red head.",
               			"My son gets bored easily.",
               			"I said I need some breaded chicken."
            			];
     
    				function doCheck()
    				{
    
    					var checkNum = text.join(' ').match(/\b[a-z]+ed\b/g);
        
    					document.getElementById( "textarea" ).value = "The words ending in 'ed' are: " '\n\n'+checkNum.join(', ');
    				}
    			    //-->
    		</script>
    	</head>
    	<body onload="doCheck()">
       		<textarea id="textarea" name="textarea" rows="10" cols="40"></textarea>
    	</body>
    </html>

    It's like this is a freakin' impossible problem using what we've learned so far. I don't see us needing complex code because we haven't covered anything complex. This isn't even a JavaScript class persay, it's an Internet programming class, UCK! I don't want to lose my passion for programming!!!!!

  • #11
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    This is a working script that I just tested

  • #12
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    File didn't attach before
    Attached Files Attached Files


  •  

    Posting Permissions

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