Jump to content

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


wizi
 Share

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

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