...

View Full Version : Searching script



babylikesburgh
08-07-2008, 05:35 PM
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!! :thumbsup:



<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>

ninnypants
08-07-2008, 05:44 PM
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:


regx = /ed(' '|\.|$|\.$)/i;

babylikesburgh
08-07-2008, 10:34 PM
Is this the part I'm supposed to change:


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

?

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

ninnypants
08-07-2008, 11:47 PM
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

mrhoo
08-08-2008, 12:10 AM
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

babylikesburgh
08-08-2008, 02:23 AM
I want it to display in the text area, so I tried the following... The text area shows, but nothing displays in it. :confused:


<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>

ninnypants
08-08-2008, 03:12 AM
You were missing this single quote

document.getElementById( "textarea" ).value = "The words ending in 'ed' are: "+ '\n\n'+checkNum.join(', ');

babylikesburgh
08-08-2008, 03:26 AM
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


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! :eek:

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!!! :)

ninnypants
08-08-2008, 03:29 AM
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

babylikesburgh
08-08-2008, 03:55 PM
Ok, I tried this:


<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:


<?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!!!!!

ninnypants
08-08-2008, 04:11 PM
This is a working script that I just tested

ninnypants
08-08-2008, 04:13 PM
File didn't attach before



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum