Javascript测试数组的角落(网格)(Javascript Testing Corners of an Array (Grid))

编程入门 行业动态 更新时间:2024-10-24 22:27:59
Javascript测试数组的角落(网格)(Javascript Testing Corners of an Array (Grid))

我正在尝试重现Schelling的Segregation模型。 我有一个函数(下面)正在测试,看看阵列的四个直接相邻的单元格是否相同或不同,或者与当前正在测试的单元格相比是空的。

对于阵列中的每个单元,有四个可能的测试点。 但是在角落和侧面斑点上,显然你无法测试超出范围的空间。 所以在函数中,如果它找到一个超出边界的空间,它会减少单元格周围的数字总数。 然而,它一直崩溃告诉我,我有一个未捕获的参考错误:无法读取未定义的属性'0'。 我不知道为什么会崩溃。

此代码的最后一行采用商品数量(类似单元格)及其周围的单元格总数(空单元格不计算)并获得相似的百分比。

任何帮助将被告知我为什么它可能会崩溃并给我一个错误? 谢谢!

model.Test = function( i, j ) { var numberToTest= 4; var goods= 0; if ((i - 1) >= 0) { if (model.BoardArray[i-1][j] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i-1][j]) { goods++; } } else { numberToTest--; } } else { numberToTest--; } if((i + 1) < $("#BoardSizeValue").val()) { if (model.BoardArray[i+1][j] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i+1][j]) { goods++; } } else { numberToTest--; } } else { numberToTest--; } if ((j - 1) >= 0) { if (model.BoardArray[i][j-1] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i][j-1]) { goods++; } } else { numberToTest--; } } else { numberToTest--; } if ((j + 1) < $("#BoardSizeValue").val()) { if (model.BoardArray[i][j+1] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i][j+1]) { goods++; } } else { numberToTest--; } } else { numberToTest--; } var similar = $("#SimilarSlider").val()/100; if (numberToTest == 0) { return false; } else { var needed = goods/numberToTest; if (needed >= similar) { return false; } else { return true; } } }

I'm doing this project trying to reproduce Schelling's Segregation model. I have a function(below) that is testing to see if the four immediate adjacent cells of the array are either the same or different or empty compared to the current cell being tested.

There are four possible spots to be tested for every cell in the array. But on corners and side spots, obviously you cant test spaces that are out of bounds. So in the function, if it finds one of the out of bounds spaces it decrements the number total around the cell. However, it keeps crashing telling me that I have an Uncaught Reference Error: Cannot read property '0' of undefined. I can't tell why its crashing.

The final lines of this code take the number of goods(similar cells) and the total number of cells around it (empty cells do not count) and gets a percentage similar.

Any help would be appreciated into telling me why it might be crashing and giving me an error? Thanks!

model.Test = function( i, j ) { var numberToTest= 4; var goods= 0; if ((i - 1) >= 0) { if (model.BoardArray[i-1][j] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i-1][j]) { goods++; } } else { numberToTest--; } } else { numberToTest--; } if((i + 1) < $("#BoardSizeValue").val()) { if (model.BoardArray[i+1][j] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i+1][j]) { goods++; } } else { numberToTest--; } } else { numberToTest--; } if ((j - 1) >= 0) { if (model.BoardArray[i][j-1] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i][j-1]) { goods++; } } else { numberToTest--; } } else { numberToTest--; } if ((j + 1) < $("#BoardSizeValue").val()) { if (model.BoardArray[i][j+1] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i][j+1]) { goods++; } } else { numberToTest--; } } else { numberToTest--; } var similar = $("#SimilarSlider").val()/100; if (numberToTest == 0) { return false; } else { var needed = goods/numberToTest; if (needed >= similar) { return false; } else { return true; } } }

最满意答案

从查看代码,您只会得到一个Reference Error: Cannot read property '0' of undefined. 如果i超出阵列的范围。

我认为问题可能出在这部分代码中:

if ((i - 1) >= 0) { if (model.BoardArray[i-1][j] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i-1][j]) {

如果i = $("#BoardSizeValue").val()和$("#BoardSizeValue").val()是数组大小的一个索引,那么[i-1]就可以了,但不是[i] 。 所以尝试将代码调整为:

if ((i - 1) >= 0 && i < $("#BoardSizeValue").val()) { if (model.BoardArray[i-1][j] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i-1][j]) {

这也适用于j比较。

From looking at your code, you would only get a Reference Error: Cannot read property '0' of undefined. if i was out of the bounds of the array.

I think the problem might be in this part of the code:

if ((i - 1) >= 0) { if (model.BoardArray[i-1][j] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i-1][j]) {

if i = $("#BoardSizeValue").val() and $("#BoardSizeValue").val() is a one-based index of the array size, then [i-1] would be okay, but not [i]. So try adjusting your code to this:

if ((i - 1) >= 0 && i < $("#BoardSizeValue").val()) { if (model.BoardArray[i-1][j] != "E") { if (model.BoardArray[i][j] == model.BoardArray[i-1][j]) {

This would also apply to the j comparisons as well.

更多推荐

本文发布于:2023-07-29 20:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319377.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   数组   角落   测试   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!