Click to See Complete Forum and Search --> : [RESOLVED] Javascript problem


zeid
12-04-2006, 12:32 PM
Q1- Hi all, I have a project that there is a page with textbox and textarea and 2 buttons, I should write a string (word or more) in the textbox then when I click on the "Search" button it should open a window.prompt, which I should enter a char or word and then it should return how many times this word or character is found in the string entered in the textbox and then show it in the textarea, Here is what I have but it's not working:


<html>
<head>
<title>Page 1</title>
<script type="text/javascript">

function search()
{
var a = window.prompt("Enter the string you want to search for:",0);
var b = F1.T1.value;
var count = 0;
for (var i = 1; i <= b.length; i++)
{
if (b.indexOf(a,i))
{
count = count + 1;
}
}
F1.A1.value = count;
}
</script>
</head>

<body>
<br /><br />
<form name="F1">
<p>Enter value:</p>
<input type="text" name="T1" size="20" />
<input type="button" name="S1" value="Search" onclick="search()" />
<input type="button" name="S2" value="Show" onclick="show()" />
<br />
<p>This is the result:</p>
<textarea cols="30" rows="6" name="A1" /></textarea>
</form>
</body>
</html>



Q2- There is another button named as "Show" .. it should take the string in the textbox and return it like this: (using a function called "show()"..

Ex: if the string is "world" then it should write the following in the textarea:

w
wo
wor
worl
world

So can someone help me please with these two questions,
Thanks in advance,
zeid

A1ien51
12-04-2006, 02:33 PM
strings begin at index 0 not 1 and they end at length-1 because of the shift.

Second question, start coding....

Eric

zeid
12-05-2006, 04:31 AM
ok but there is a problem, that this code is giving me the length of "a" ( or the string entered ) not how many times is "b" found in a..

zeid
12-05-2006, 05:34 AM
I have fixed it.. thanks