Jump to content

Please Help! Typeerror: Array[i] is undefine?


wizi

Recommended Posts

 function setDefaultScores (s1, s2) //s1: vertical, s2: horizontal, (i=s1; j=s2) { 	var arrayDS = new Array();  	for (var i = 0; i < s1.length; i++) 	{ 		for (var j =0; i < s2.length; j++) 		{ 			if (s1.charAt(i) == s2.charAt(j)) 			{ 				arrayDS[i][j] =1; 			} 			else 			{ 				arrayDS[i][j] = 0; 			} 		} 	} }

When I call this function. I got an error "Typeerror: arrayDS is undefined." at arrayDS[j] =1;Can someone show me how to solve this? I don't see anything wrong with this function. Even when I change arrayDS[0] = 1; it still gives me the same error. Thank you very much.

Link to comment
Share on other sites

You have just one dimensional array. In other words, each array member must also be an array itself, and you must define it at every level.Try it like that:

function setDefaultScores (s1, s2) //s1: vertical, s2: horizontal, (i=s1; j=s2){	var arrayDS = new Array(); 	for (var i = 0; i < s1.length; i++)	{		arrayDS[i] = new Array();		for (var j =0; i < s2.length; j++)		{			if (s1.charAt(i) == s2.charAt(j))			{				arrayDS[i][j] =1;			}			else			{				arrayDS[i][j] = 0;			}		}	}}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...