Jump to content

Javascript problem


zeidhaddadin
 Share

Recommended Posts

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 = 0; i <= b.length -1; 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:wwoworworlworldSo can someone help me please with these two questions,Thanks in advance,zeid

Link to comment
Share on other sites

any help?? .. btw this program is giving me the length of the string ( a ) not the count of ( b ) string in ( a)..
Try changing your if statement from:
if (b.indexOf(a,i))

To:

if (b.indexOf(a,i) != -1)

Or look at charAt().

Link to comment
Share on other sites

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:wwoworworlworld
Try this for show()
function show(){ var b = F1.T1.value; var str = ""; for (var i = 0; i <= b.length -1; i++)  {	str = str + b.substring(0,i+1);	str = str + '\n';  }  F1.A1.value = str;}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...