JS While循环忽略And条件?

huangapple go评论70阅读模式
英文:

JS While loop ignoring And condition?

问题

以下是已翻译的内容:

给定以下代码来查找毕达哥拉斯三元组,为什么我会得到重复的结果,而不是通过While循环后跟If条件来排除它们?

var a, b, c, i, pyTrips, temp;
pyTrips = new Array();
temp = new Array();

for (a = 1; a <= 10; a++) {
    for (b = 1; b <= 10; b++) {
        with (Math) {
            c = sqrt(pow(a, 2) + pow(b, 2));
        }
        if (c % 1 == 0) {
            temp = a < b ? [a, b, c] : [b, a, c];
            i = 0;
            while (i < pyTrips.length && pyTrips[i] !== temp) {
                i++;
            }
            if (i === pyTrips.length) {
                pyTrips.push(temp);
            }
            console.log(pyTrips.toString());
        }
    }
}

我假设问题出在 pyTrips[i]!==temp 部分,但我已经检查过了,没有类型转换或其他问题;实际上是在两个数组之间进行比较。所以不确定为什么While循环似乎每次都达到了 pyTrips.length

英文:

Given the following code to find <a href="https://en.wikipedia.org/wiki/Pythagorean_triple">Pythagorean Triples</a>, why am I getting duplicates rather than excluding them with the While loop followed by the If condition?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var a, b, c, i, pyTrips, temp;
pyTrips = new Array();
temp = new Array();

for (a = 1; a &lt;= 10; a++) {
    for (b = 1; b &lt;= 10; b++) {
        with (Math) {
            c = sqrt(pow(a, 2) + pow(b, 2));
        }
        if (c % 1 == 0) {
            temp = a &lt; b ? [a, b, c] : [b, a, c];
            i = 0;
            while (i &lt; pyTrips.length &amp;&amp; pyTrips[i] !== temp) {
                i++;
            }
            if (i === pyTrips.length) {
                pyTrips.push(temp);
            }
            console.log(pyTrips.toString());
        }
    }
}

<!-- end snippet -->

I assume it's the pyTrips[i]!==temp part that's tripping things up, but I've checked and there's no type casting or anything; the comparison is in fact between two arrays. So not sure why the While loop seems to make it to pyTrips.length every time.

答案1

得分: 2

JavaScript数组是对象。对象是通过比较它们的对象引用来比较的,而不是比较它们的内容。

你可以通过在两个数组上使用toString来实现这一点:

while(i < pyTrips.length && pyTrips[i].toString() !== temp.toString()) { i++; }
英文:

Javascript arrays are objects. Objects are compared by comparing their object references, NOT by comparing their contents.

You can do this by using toString on both arrays:

while(i&lt;pyTrips.length &amp;&amp; pyTrips[i].toString()!==temp.toString()){ i++; }

答案2

得分: 1

你的假设是正确的,你不能直接比较两个数组。当你使用 === 运算符比较两个数组时,它比较的是它们的引用,而不是它们的内容。

解决这个问题的一种方法是使用 toString(),将两个数组转换为字符串,然后进行比较

pyTrips[i].toString() !== temp.toString()

另一种方法是使用一个比较数组的实用函数

const isEqualArr = (arr1, arr2) => {
    if (arr1.length !== arr2.length) {
        return false;
    }

    return arr1.every((ele, index) => ele === arr2[index]);
};

然后像这样使用它

!isEqualArr(pyTrips[i], temp)
英文:

Your assumption is correct, you cannot compare two arrays directly. When you compare two arrays using the === operator, it compares their references, not their content.

One hacky way to solve this is to use toString(), convert the two arrays to string and then compare

pyTrips[i].toString() !== temp.toString()

Another way is to use a utility function which compares arrays

const isEqualArr = (arr1, arr2) =&gt; {
    if (arr1.length !== arr2.length) {
        return false;
    }

    return arr1.every((ele, index) =&gt; ele === arr2[index]);
};

And then use it like

!isEqualArr(pyTrips[i], temp)

答案3

得分: 1

你可以尝试在每次成功迭代后清空数组:

var a, b, c, i, pyTrips, temp;
pyTrips = new Array();
temp = new Array();

for (a = 1; a <= 10; a++) {
    for (b = 1; b <= 10; b++) {
        with (Math) {
            c = sqrt(pow(a, 2) + pow(b, 2));
        }
        if (c % 1 == 0) {
            temp = a < b ? [a, b, c] : [b, a, c];
            i = 0;
            while (i < pyTrips.length && pyTrips[i] !== temp) {
                i++;
            }
            if (i === pyTrips.length) {
                pyTrips.push(temp);
            }
            console.log(pyTrips.toString());
            pyTrips = [];
        }
    }
}
英文:

You can try clearing the array after each successful iteration:

var a, b, c, i, pyTrips, temp;
pyTrips = new Array();
temp = new Array();

for (a = 1; a &lt;= 10; a++) {
    for (b = 1; b &lt;= 10; b++) {
        with (Math) {
            c = sqrt(pow(a, 2) + pow(b, 2));
        }
        if (c % 1 == 0) {
            temp = a &lt; b ? [a, b, c] : [b, a, c];
            i = 0;
            while (i &lt; pyTrips.length &amp;&amp; pyTrips[i] !== temp) {
                i++;
            }
            if (i === pyTrips.length) {
                pyTrips.push(temp);
            }
            console.log(pyTrips.toString());
            pyTrips = [];
        }
    }
}

huangapple
  • 本文由 发表于 2023年8月9日 12:49:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864663-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定