Javascript infinite loop problem[SOLVED]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Javascript infinite loop problem[SOLVED]

Post by raghavan20 »

I have a string which has to be parsed and made into tokens using the set of arrays defined inside the function.
most of the things work fine, i have even identified a problem.
the solution shd be to set the variable named match as 0 every looping process....but when i make it to zero....the script goes into infinite loop...
i have attached enough comments to understand the script and plz dont look into issubstring fn as there is no problem there....
when trying:
jus give inputs to the input text control
ex:
inputs shd be: a, aa, aab, aac, aabcd, bcccdaab,
ouputs shd be: aa, aa;b, aa;c, aa;b;c;d, b;ccc;d;aa;b
here is the code...

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Document Object Model Table Sample</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-8859-1">
</HEAD>
<BODY>



<!-- Placeholder for the table -->
<div id="top" align="center">
	<div id="nested" style="background-color:#CCCCCC " onclick="killme();"> hi all </div>
	<table id="oTable">
		<tr><td onclick="killme()">kill me</td></tr>
	</table>
</div>
<input type="text" onKeyUp="display_combinations(this.value);" onBlur="process_text(this.value);" />
<div id="display_text" style="background-color:#FFCC33; color:#000000; ">
my lord</div>
<script type="text/javascript">
	function process_text(value){
		//alert("process_text function called!!!");
		
		/* Obtain the reference of the div object*/
		var display_text_ref = document.getElementById("display_text");
		
		/* The string that would be assigned to the divs' innerHTML */
		var display_string = "";
		
		/* Split the string into chunks whenever a blank character comes up */
		var individual_chunks_array = new Array();
		individual_chunks_array = value.split(' ');
		
		/* Process each chunk and parse into appropriate tokens using the array in the other function */
		var individual_chunks_array_length = individual_chunks_array.length;
		for(var i = 0; i < individual_chunks_array_length; i++){
			display_string += " " + process_chunks(individual_chunks_array[i]);
		}
		
		/* Assign the set of tokens returned to the div */
		display_text_ref.innerHTML = display_string;
	}
	
	function process_chunks(start_character){
		//alert("process_chunks function called!!!");
		/* Declaring new array */
		var strings_array = new Array(5);
		
		/* adding elements to the strings_array */
		strings_array[0] = new Array(3);
		strings_array[0][0] = "a-A";
		strings_array[0][1] = "aa-AA";
		strings_array[0][2] = "aaa-AAA";
	
		strings_array[1] = new Array(3);
		strings_array[1][0] = "b-B";
		strings_array[1][1] = "bb-BB";
		strings_array[1][2] = "bbb-BBB";
	
		strings_array[2] = new Array(3);
		strings_array[2][0] = "c-C";
		strings_array[2][1] = "cc-CC";
		strings_array[2][2] = "ccc-CCC";
	
		strings_array[3] = new Array(3);
		strings_array[3][0] = "d-D";
		strings_array[3][1] = "dd-D";
		strings_array[3][2] = "ddd-DDD";
	
		strings_array[4] = new Array(3);
		strings_array[4][0] = "e-E";
		strings_array[4][1] = "ee-EE";
		strings_array[4][2] = "eee-EEE";
		
	
		/* Initially set the number of matches found */
		/* Counters for two for loops */
		var i, j;
		
		/*Used to extract the second part of every array element*/
		var array_last_part = "";
		
		/* Initially no match for the start_character is found */
		var matches = 0; 
		
		/* matches should be found for each token; counts the number of chars which are already made as tokens */
		var z = 0;
		
		/* The string that would be used inside the computation blocks; will always be a substring of start_characteer*/
		var compare_string = start_character;
		
		/* times denotes the number of times the substring is pruned because of no match */
		var times = start_character.length;
		
		/* stores the values of the parsed tokens */
		var temp_array = Array();
		var y = 0; //counter for the above array
		
		/* used to display comments to the user screen */
		var temp = "";
		temp += "<br />Chunk to be processed: " + start_character + "<br /><br /><br /><br />";
		
		/* Continue the loop until all the characters have been matched/converted as tokens */
		while(z != start_character.length){
			/* Traverse the entire array and look for matches of the string paseed in to be searched */
			for (i = 0; i < strings_array.length; i++){
				for(j = 0; j < strings_array[i].length; j++){
					/* find the last part of an array element, that is the value */
					array_last_part = strings_array[i][j].substr(0, strings_array[i][j].indexOf("-"));
					if (is_substring(array_last_part, compare_string)){
						/* If a match is found, then set the match variable */
						matches = 1;
					}
				}
			}
			/*
			* If a match is found,
			* store the token; for the next part of the string to be matched; form an appropriate comment
			*/
			if(matches == 1){
				//matches = 0; //==========>here is the problem; if i set this, it goes into infinite loop
				temp_array[y++] = compare_string;
				z = z + (compare_string.length);
				compare_string = start_character.substr((compare_string.length), (start_character.length - compare_string.length));
				temp += "<br /><br />Inside true: temp_array[" + (y-1) + "]: " + temp_array[y-1] + "<br />times: " + times + "<br />compare_string: " + compare_string + "<br /> z:" + z;
			}
			/*
			* if not found,
			* bring down the size of the string by 1 and keep the count of the times you have brought down the length
			*/
			else{
				times = times - 1;
				compare_string = compare_string.substr(0, times);
				temp += "<br />Inside false: Incrementing times:" + times + "<br />new compare_string: " + compare_string;
			}
			
		}
		temp += "<br /><br /><br />temp arrays elements:";
		
		/* for display, store the tokens into a string */
		for (i = 0; i < temp_array.length; i++)
			temp += "element: " + i + " ===>" + temp_array[i] + "<br />";
		return temp;
	
	}

function is_substring(main_string, search_string){
	var search_string_length = search_string.length;
	var main_string_length = main_string.length;
	var i, j, temp="", valid=1;

	temp += "<br /><b>New search</b><br /><i>INPUTS:</i>Main String:" + main_string + "; Search string:" + search_string ;
	temp += "<br />Main string length:" + main_string.length + "; Search string length:" + search_string.length ;
	if (search_string.length == 0 || (search_string.length > main_string.length)){
		valid = 0;
		//DEBUG//temp += "<br />String can not be processed<br /><br /><br />";
		//DEBUG//document.write(temp);
		return false;
	}else{
		for (i = 0; i < main_string_length - (search_string_length - 1); i++){
			for (j = 0; j < search_string_length; j++){
				temp += "<br />searched character--------->" + search_string.substr(j, 1);
				if (main_string.substr(j+i, 1) != search_string.substr(j, 1)){
					//temp += "<br />Found===>" + (j+i) + "-" + search_string.substr(j, 1)  + "-" + j + "<br />";
					valid = 0;
					break;
				}else{
					temp += "<br />MATCH===>Main string position: " + (j+i) + "- Matched char:" + search_string.substr(j, 1)  + "-  Search string match pos:" + j + "<br />";
				}
			}
			/*Do not continue any further searching if already found */
			if (valid == 1){
				break;
			}
		}
	}

	if(valid==1){
		//document.write(temp + "<br />Final validity of a string:" + valid + "<br /><br /><br />");
		return true;
	}else{
		return false;
	}
}

</script>
</body>
</html>
Last edited by raghavan20 on Mon Oct 17, 2005 7:34 pm, edited 1 time in total.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I have solved the problem with a different logic.
Thanks for all whoever viewed the problem.

Code: Select all

function process_text(value){
		//alert("process_text function called!!!");
		
		/* Obtain the reference of the div object*/
		var display_text_ref = document.getElementById("display_text");
		
		/* The string that would be assigned to the divs' innerHTML */
		var display_string = "";
		
		/* Split the string into chunks whenever a blank character comes up */
		var individual_chunks_array = new Array();
		individual_chunks_array = value.split(' ');
		
		/* Process each chunk and parse into appropriate tokens using the array in the other function */
		var individual_chunks_array_length = individual_chunks_array.length;
		for(var i = 0; i < individual_chunks_array_length; i++){
			display_string += " " + process_chunks(individual_chunks_array[i]);
		}
		
		/* Assign the set of tokens returned to the div */
		display_text_ref.innerHTML = display_string;
	}
	
	function process_chunks(start_character){
		//alert("process_chunks function called!!!");
		/* Declaring new array */
		var strings_array = new Array(5);
		
		/* adding elements to the strings_array */
		strings_array[0] = new Array(3);
		strings_array[0][0] = "a-A";
		strings_array[0][1] = "aa-AA";
		strings_array[0][2] = "aaa-AAA";
	
		strings_array[1] = new Array(3);
		strings_array[1][0] = "b-B";
		strings_array[1][1] = "bb-BB";
		strings_array[1][2] = "bbb-BBB";
	
		strings_array[2] = new Array(3);
		strings_array[2][0] = "c-C";
		strings_array[2][1] = "cc-CC";
		strings_array[2][2] = "ccc-CCC";
	
		strings_array[3] = new Array(3);
		strings_array[3][0] = "d-D";
		strings_array[3][1] = "dd-D";
		strings_array[3][2] = "ddd-DDD";
	
		strings_array[4] = new Array(3);
		strings_array[4][0] = "e-E";
		strings_array[4][1] = "ee-EE";
		strings_array[4][2] = "eee-EEE";
		
	
		/* Initially set the number of matches found */
		/* Counters for two for loops */
		var i, j;
		
		/*Used to extract the first part of every array element*/
		var array_first_part = "";
		
		/* Initially no match for the start_character is found */
		var matches = 0; 
		
		/* matches should be found for each token; counts the number of chars which are already made as tokens */
		var z = 0;
		
		/* The string that would be used inside the computation blocks; will always be a substring of start_characteer*/
		var compare_string = start_character;
		
		/* times denotes the number of times the substring is pruned because of no match */
		var times = 0;
		
		/* stores the values of the parsed tokens */
		var temp_array = Array();
		var y = 0; //counter for the above array
		
		/* used to display comments to the user screen */
		var temp = "", temp1 = "", matched_string = ""; 
		
		/* Look for string matches for the input string */
		while(z < start_character.length){
			//temp += "Current loop compare string:" + compare_string;
			//matches = 0;
			for (i = 0; i < strings_array.length; i++){
				for(j = 0; j < strings_array[i].length; j++){
					/* find the last part of an array element, that is the value */
					array_first_part = strings_array[i][j].substr(0, strings_array[i][j].indexOf("-"));
					if (array_first_part == compare_string){
						//DEBUG//temp += "MATCHED STRING:::::::ARRAY_LAST_PART:" + array_last_part + "<br />COMPARE_STRING:" + compare_string;
						matches = 1;
						matched_string = strings_array[i][j].substr((strings_array[i][j].indexOf("-") + 1), strings_array[i][j].length);
					}
				}
			}
			if (matches == 1){
				matches = 0;
				temp_array[y++] = matched_string;
				z = z + (compare_string.length);
				compare_string = start_character.substr(z, (start_character.length - z));
				times = 0;
				//DEBUG//temp += "<br /><br />Inside true: temp_array[" + (y-1) + "]: " + temp_array[y-1] + "<br />times: " + times + "<br />compare_string: " + compare_string + "<br /> z:" + z;
			}else{
				times++;
				compare_string =compare_string.substr(0, (start_character.length - times));
				//DEBUG//temp += "<br />Inside false: Incrementing times:" + times + "<br />new compare_string: " + compare_string;
			}
			
		}
		//DEBUG//temp += "<br /><br /><br />temp arrays elements:";
		for (i = 0; i < temp_array.length; i++)
			//temp += "element: " + i + " ===>" + temp_array[i] + "<br />";
			temp += temp_array[i];
		return temp;
	
	}
	
</script>
Post Reply