admin管理员组文章数量:1312957
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
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
Share Improve this question asked Mar 16, 2013 at 23:24 scrblnrd3scrblnrd3 7,4269 gold badges36 silver badges65 bronze badges3 Answers
Reset to default 6You 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);
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;
}
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++;
}
}
本文标签: arraysplice not working correctly in javascriptStack Overflow
版权声明:本文标题:array.splice not working correctly in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741902350a2403941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论