刚刚开始使用JavaScript中的基本网格分析算法,但我遇到了一个令我困惑的错误。
var max = 9;
var testArray = new Array(
['7', '3', '9', '6', '4', '1', '5', '2', '8'],
['1', '8', '2', '7', '5', '3', '4', '6', '9'],
['9', '5', '7', '3', '8', '2', '1', '4', '6'],
['3', '1', '4', '9', '6', '7', '2', '8', '5'],
['6', '2', '8', '5', '1', '4', '9', '3', '7'],
['5', '4', '6', '2', '9', '8', '3', '7', '1'],
['8', '7', '1', '4', '3', '5', '6', '9', '2'],
['2', '9', '3', '1', '7', '6', '8', '5', '4']
);
function checkYoSelf(myGrid) {
var i; var j;
var horizLine = new String;
for( i = 0; i <= (max - 1); i++ ) {
for( j = 0; j <= (max - 1); j++) {
document.write(i+"<br />");
horizLine += myGrid[i][j];
}
var test = RegExp(i, "ig");
var result = new Array(horizLine.match(test));
if( result.length > 1 ) {
alert("fail");
}
}
}
html file has <a href='#' onclick="checkYoSelf(testArray);">check</a>
根据萤火虫myGrid[i]
未定义,但我不确定为什么会这样。
我做错了什么?
答案 0 :(得分:2)
嗯,这对我有用......我刚刚将“max”变量替换为更具动态性的变量:
<script type="text/javascript">
var testArray = new Array(
['7', '3', '9', '6', '4', '1', '5', '2', '8'],
['1', '8', '2', '7', '5', '3', '4', '6', '9'],
['9', '5', '7', '3', '8', '2', '1', '4', '6'],
['3', '1', '4', '9', '6', '7', '2', '8', '5'],
['6', '2', '8', '5', '1', '4', '9', '3', '7'],
['5', '4', '6', '2', '9', '8', '3', '7', '1'],
['8', '7', '1', '4', '3', '5', '6', '9', '2'],
['2', '9', '3', '1', '7', '6', '8', '5', '4']
);
function checkYoSelf(myGrid) {
var i;
var j;
var horizLine = new String;
var maxRows = myGrid.length;
for( i = 0; i < maxRows; i++ ) {
var maxColumns = myGrid[i].length;
for( j = 0; j < maxColumns; j++) {
document.write(i+"<br />");
horizLine += myGrid[i][j];
}
var test = RegExp(i, "ig");
var result = new Array(horizLine.match(test));
if( result.length > 1 ) {
alert("fail");
}
}
}
</script>
<a href='#' onclick="checkYoSelf(testArray);">check</a>
不确定您要对此做些什么,但至少它不会给出任何错误。
答案 1 :(得分:1)
max
来自哪里?
max是全局定义的(在本例中为max = 9)
你只有8行。
答案 2 :(得分:0)
从你的评论到nickf的答案:这是因为max是9,但你在阵列中只有8行。
答案 3 :(得分:0)
Firebug说
max未定义
当最后一个索引是testArray [7]时,你试图在checkYoSelf(testArray)中访问testArray [8]。
答案 4 :(得分:0)
testArray
包含8个元素。您正在访问testArray[8]
,这是无效的,因为索引必须是0..7(0..8-1)。在数组中添加另一行,或者将第一行重写为:
for( i = 0; i <= (max - 2); i++ ) {
答案 5 :(得分:0)
如果你改写
for( i = 0; i <= (max - 1); i++ ) {
for( j = 0; j <= (max - 1); j++) {
作为
for( i = 0; i < (max - 1); i++ ) {
for( j = 0; j < max; j++) {
它应该有用。网格的第一维只有8。
答案 6 :(得分:0)
感谢大家的帮助,现在代码是:
,现在不再发生萤火虫错误var testArray = new Array(
['7', '3', '9', '6', '4', '1', '5', '2', '8'],
['1', '8', '2', '7', '5', '3', '4', '6', '9'],
['9', '5', '7', '3', '8', '2', '1', '4', '6'],
['3', '1', '4', '9', '6', '7', '2', '8', '5'],
['5', '4', '6', '2', '9', '8', '3', '7', '1'],
['6', '2', '8', '5', '1', '4', '9', '3', '7'],
['8', '7', '1', '4', '3', '5', '6', '9', '2'],
['2', '9', '3', '1', '7', '6', '8', '5', '4'],
['2', '9', '3', '1', '7', '6', '8', '5', '4']
);
function checkYoSelf(myGrid) {
var i;
var j;
var horizLine = new String;
var max = myGrid.length;
for( i = 0; i < max; i++ ) {
for( j = 0; j < max; j++) {
horizLine += myGrid[i][j];
}
var test = RegExp(i, "ig");
var result = new Array(horizLine.match(test));
if( result.length > 1 ) {
alert("fail");
}
}
}