Reputation: 601
I've nearly solved this... It's Project Euler's 4th problem and the answer is supposed to be 906609 but I keep getting 580085. I'd appreciate any help.
Code:
function largestPalin() {
var a, b, c;
for (a = 999; a > 100; a--) {
for (b = a; b > 100; b--) {
c = a * b;
if (palinCheck(c)) {
return c;
}
}
}
function palinCheck(c) {
return c == c.toString().split("").reverse().join("");
}
}
console.log(largestPalin());
Upvotes: 1
Views: 138
Reputation: 1878
Instead of returning the first result you find, you should save it if it's larger than the last one you found and return it at the end.
var largestFound = 0;
for (a = 999; a > 100; a--) {
for (b = a; b > 100; b--) {
c = a * b;
if (palinCheck(c)) {
if (c > largestFound) {
largestFound = c;
}
}
}
}
return largestFound;
Upvotes: 3