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.
我正在编写一个简单的数独求解器,它采用数字1-9的数组,并且如果它们不可能用于该单元格则将它们设置为空。一个例子是一个单元格,其答案只能是5,所以所有数字都设置为null,除了5。然后,我有一个clean()函数,它删除数组中的所有值为null,但这是无法正常工作。原始数组就是这个。
[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
javascript代码在这里,网格是数独中的数字网格
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
从本质上讲,array.splice并没有拼接所需的一切,我不知道为什么
3 个解决方案
#1
6
You change the array while you iterate. Try something like that:
您在迭代时更改数组。试试这样的事情:
function clean(array){
for(var i=0;i<=8;i++){
if(array[i]===null){
array.splice(i--,1);
}
}
return array;
}
The --
lower the index because the next item will then be at the same index than the item you are removing.
- 降低索引,因为下一个项目将与您要删除的项目处于相同的索引。
Moreover, objects and arrays passed as argument are passed by reference so you don't need to return anything. You can do clean(possibs);
此外,作为参数传递的对象和数组通过引用传递,因此您不需要返回任何内容。你可以干净(possibs);
#2
2
That's because when you "splice" the array , the index change. Maybe you can try this code :
那是因为当你“拼接”数组时,索引会发生变化。也许你可以试试这段代码:
function clean(array){
var x = [];
for(var i=0;i<array.length;i++){
if(array[i]!=null){
x.push(array[i]);
}
}
return x;
}
#3
0
try this:
尝试这个:
var array = [null,null,null,null,5,null,null,null,null];
for(var i=0;i<=array.length; ){
if(array[i] === null){
array.splice(i,1);
} else if (array.length < 2) {
break;
} else {
i++;
}
}
#1
6
You change the array while you iterate. Try something like that:
您在迭代时更改数组。试试这样的事情:
function clean(array){
for(var i=0;i<=8;i++){
if(array[i]===null){
array.splice(i--,1);
}
}
return array;
}
The --
lower the index because the next item will then be at the same index than the item you are removing.
- 降低索引,因为下一个项目将与您要删除的项目处于相同的索引。
Moreover, objects and arrays passed as argument are passed by reference so you don't need to return anything. You can do clean(possibs);
此外,作为参数传递的对象和数组通过引用传递,因此您不需要返回任何内容。你可以干净(possibs);
#2
2
That's because when you "splice" the array , the index change. Maybe you can try this code :
那是因为当你“拼接”数组时,索引会发生变化。也许你可以试试这段代码:
function clean(array){
var x = [];
for(var i=0;i<array.length;i++){
if(array[i]!=null){
x.push(array[i]);
}
}
return x;
}
#3
0
try this:
尝试这个:
var array = [null,null,null,null,5,null,null,null,null];
for(var i=0;i<=array.length; ){
if(array[i] === null){
array.splice(i,1);
} else if (array.length < 2) {
break;
} else {
i++;
}
}