可笑的 javascript bug:必须多次调用函数才能正确运行
我毫不怀疑这是我自己的愚蠢造成的。我不是程序员,而是科学家,我通常只是不断地研究一些东西,直到它起作用,这就是我最终遇到如此奇怪的错误的原因。基本上,任何帮助都会非常感激。
好的,所以我的功能是这样的:
function discardDuplicates(threshold) {
for (var m = 0; m < xCo2.length; m++){
var testX = xCo2[m];
var testY = yCo2[m];
for (var n = 0; n < xCo2.length; n++){
if (m != n) {
if ((Math.abs(xCo2[n] - testX) < threshold)
&& (Math.abs(yCo2[n] - testY) < threshold)
&& deltas[m] > deltas[n]){
xCo2.splice(n,1);
yCo2.splice(n,1);
deltas.splice(n,1);
}
}
}
}
}
我正在检测具有存储在 xCo2 和 yCo2 数组中的坐标(x,y)的特征,每个坐标还有一个称为“delta”的属性。我想检查一下我是否在基本相同的位置识别了多个功能 - 如果有,它们可能是重复的,因此我从列表中删除了除具有最高增量的功能之外的所有功能。
是的,基本上,这是行不通的!
目前我必须这样做:
//ugly hack
var oldLength = 0;
var newLength = 1;
while (oldLength != newLength) {
oldLength = xCo2.length;
discardDuplicates(10);
newLength = xCo2.length;
}
因为第一次调用该函数时它不会删除任何重复项。第二次它会删除其中的大部分。第三次它通常会拥有所有这些......所以我只是继续调用它,直到它停止删除重复项。该功能确实有效;它正在删除正确的点。
我问这个问题的原因是,同样的错误现在已经第二次发生,使用第二个函数,这次尝试删除增量值太低的任何坐标。
不胜感激!
I have no doubt at all that my own idiocy is responsible for this. I'm not a programmer, but a scientist, and I just generally hack away at something until it works which is how I end up with such weird bugs. Basically, any help would be really appreciated.
Okay, so my function is this:
function discardDuplicates(threshold) {
for (var m = 0; m < xCo2.length; m++){
var testX = xCo2[m];
var testY = yCo2[m];
for (var n = 0; n < xCo2.length; n++){
if (m != n) {
if ((Math.abs(xCo2[n] - testX) < threshold)
&& (Math.abs(yCo2[n] - testY) < threshold)
&& deltas[m] > deltas[n]){
xCo2.splice(n,1);
yCo2.splice(n,1);
deltas.splice(n,1);
}
}
}
}
}
I'm detecting features which have co-ordinates (x,y) stored in xCo2 and yCo2 arrays, each co-ordinate also has a property known as "delta." And I want to check to see if I've identified several features in basically the same place--if I have, they're probably duplicates, so I remove all but the one with the highest delta from the list.
Right, basically, this doesn't work!
At the moment I have to do this:
//ugly hack
var oldLength = 0;
var newLength = 1;
while (oldLength != newLength) {
oldLength = xCo2.length;
discardDuplicates(10);
newLength = xCo2.length;
}
Because the first time the function's called it doesn't remove any duplicates. The second time it removes most of them. The third time it usually has them all... So I just keep on calling it until it stops removing duplicates. The function definitely works though; it's removing the right points.
The reason I'm asking this question is that this same bug has now happened a second time, with a second function, which this time tries to remove any co-ordinates with a delta value which is too low.
Enlightenment would be gratefully accepted!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在数组上调用 splice 会删除一个条目并将下一个条目移动到该位置,这是非常常见的错误。
我们看一下:
看到问题了吗?您正在跳过条目,您需要做的是每次从数组中删除条目时减少
n
。由于您总是删除 1 个条目,因此在拼接数组后需要将 n 减少一次。PS:一些空格大大提高了代码的可读性。
Calling
splice
on the array will remove an entry and move the next entry to this position, that's very common mistake.Let's take a look:
See the problem? You're skipping entries, what you need to do is to reduce
n
each time you remove entries from the arrays. Since you're always removing 1 entry you need to reduce n by once after you've spliced your arrays.PS: Some whitespace greatly increases the readability of your code.