I am trying to recode a sudoku game to be used in smart tv's. I want to find empty cells that the puzzle didn't fill at the beginning. I declared a 2 dimensional aray
var emptyCellsArray =new Array(9);
emptyCellsArray[0] = new Array();
emptyCellsArray[1] = new Array();
emptyCellsArray[2] = new Array();
emptyCellsArray[3] = new Array();
emptyCellsArray[4] = new Array();
emptyCellsArray[5] = new Array();
emptyCellsArray[6] = new Array();
emptyCellsArray[7] = new Array();
emptyCellsArray[8] = new Array();
write the function
function fillEmptyCellsArray() {
for ( var i= 1 ; i<=9; i++) {
for (var k=1 ; k<=9; k++) {
emptyCellsArray[i][k] = 0;
}; //k end
}; //i end
for ( var i= 1 ; i<=9; i++) {
var flag = 0;
for (var k=1 ; k<=9; k++) {
if ( !(document.getElementsByName(i+'_'+k)[0].value == "" ) ) {
//add this cell to the array
emptyCellsArray[i][flag] = 1;
flag++;
} //end if
}; //k end
}; //i end
alert("XX------" + emptyCellsArray[0][0] );
}; //function end
then called the function.
$(document).ready(function () {
fillEmptyCellsArray();
});
but i am getting this error output.
Error Detail : TypeError: document.getElementsByName(i + "_" + k)[0] is undefined
coming from this line---> if ( !(document.getElementsByName(i+'_'+k)[0].value == "" )
I am not pro, i couldn't figure it out. What is my mistake? I think the problem is in the declaration part. Can anybody enlighten me?
second version now all array filled with 1's
function fillEmptyCellsArray() {
for ( var i=0 ; i<9; i++) {
for (var k=0 ; k<9; k++) {
emptyCellsArray[i][k] = 0;
}; //k end
}; //i end
for ( var i= 0; i<9; i++) {
for (var k=0 ; k<9; k++) {
if ( !(document.getElementsByName(i+'_'+k)[0].value == "" ) ) {
//add this cell to the array
emptyCellsArray[i][k] = 1;
} //end if
}; //k end
}; //i end
for ( var i=0 ; i<9; i++) {
for (var k=0 ; k<9; k++) {
alert ( emptyCellsArray[i][k] );
}; //k end
}; //i end
}; //function end
Now your example names start at 0 and your loops start at 1. Do you have 9_9 element or is the last one 8_8.
I am guessing you really want your loops to look like this
for ( var i= 0 ; i<9; i++) {
var flag = 0;
for (var k=0 ; k<9; k++) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.