I'm writing a simple sudoku solver, which takes an array of numbers 1-9 and sets them to null if they are not possible for that cell. An example is a cell where the answer can only be a 5, so all the numbers are set to null except for five. Then, I have a clean() function which deletes all the values from the array which are null, but that is not working correctly. The original array is this.
[null,null,null,null,5,null,null,null,null]
After being cleaned, it returns
[null,null,5,null,null]
The javascript code is here, and the grid is the grid of numbers in the sudoku
function mainmethod(){
var onepos=oneposs();
}
function oneposs(){
var possibs=new Array(1,2,3,4,5,6,7,8,9);
for (var ycount=0;ycount<=8;ycount++){
var value=grid[0][ycount];
var index=possibs.indexOf(value);
possibs[index]=null;
}
// for(var xcount=0;xcount<=8;xcount++){
// var value=grid[xcount][0];
// var index=possibs.indexOf(value);
// possibs.splice(index,1);
// }
possibs=clean(possibs);
alert(JSON.stringify(possibs));
}
function clean(array){
for(var i=0;i<=8;i++){
if(array[i]===null){
array.splice(i,1);
}
}
return array;
}
Essentially, the array.splice is not splicing everything it needs to, and I don't know why