Given the following code to find Pythagorean Triples, why am I getting duplicates rather than excluding them with the While loop followed by the If condition?
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());
}
}
}
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.