英文:
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 <= 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());
}
}
}
<!-- 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<pyTrips.length && 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) => {
if (arr1.length !== arr2.length) {
return false;
}
return arr1.every((ele, index) => 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 <= 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 = [];
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论