我一直在用JavaScript编写一个数独解算器。有一个非常奇怪的行为。下面是代码和上下文:
solveSudoku = ()=>{
let sectionsCopy_ =sectionsCopy
for(let sect =0; sect< 9; sect++){
for(let box =0; box <9; box++){
if(sectionsCopy[sect][box] === ''){
for(let num=1; num<=9;num++){
if(this.isPossible(sect,box,num)){
sectionsCopy[sect][box]=''+num
this.solveSudoku()
sectionsCopy[sect][box]=''
}
}
return
}
}
}
debugger
console.log(sectionsCopy)
}
这是在类内
***sectionsCopy***是全局变量
***sectionsCopy_***仅用于调试(以便轻松查看全局var的值)
***isPossible()***是一个函数,用于确定num在该位置是否有效
***sect***是数独中3x3正方形的索引
***box***是数独中单元框的索引
问题:
请看下面的数独游戏:
单击求解时,sectionsCopy 变量将存储:
[
["2","6","" ,"4","7","" ,"5","8","1"],
["" ,"" ,"3","" ,"" ,"" ,"" ,"" ,"4"],
["" ,"1","5","" ,"" ,"8","7","6","3"],
["" ,"3","" ,"" ,"" ,"6","" ,"" ,"8"],
["4","8","9","" ,"" ,"2","3","1","" ],
["" ,"7","" ,"8","3","" ,"" ,"" ,"" ],
["6","9","" ,"3","" ,"" ,"" ,"1","" ],
["" ,"" ,"8","" ,"9","" ,"5","" ,"" ],
["" ,"" ,"7","2","" ,"" ,"" ,"9","6"]
]
当我在没有调试器的情况下运行它时,我得到了以下结果:
如果我打开调试器,我会看到:
以下是我不使用调试器时与使用调试器时的结果:
我希望调试器下的行为与没有调试器时的行为相同(否则像这种情况下将毫无用处)
我尝试了console.log(sectionsCopy_)
,但我知道数组是不可变的,所以这无关紧要
1条答案
按热度按时间igetnqfo1#
感谢@epascarello的留言。
看起来console.log在更改数据时正在输出数据。
我的解决方案是
代替