Tell us what’s happening:
Hello the code worked on jsbin return 906609 for console.log(largestPalindromeProduct(3)) but somehow it cannot pass the test.
is there some problem in my code (except inefficient and crude) that keep me from passing it?
Your code so far
function isPalin(n){
let NoD = Math.floor(Math.log10(n))+1; //number of digits
let halfNoD = Math.floor(n/2);
let dig = [];
let i;
for (i=0; i<NoD; i++){
dig[i]=n%10;
n=Math.floor(n/10);
}
for (i=0;i<halfNoD; i++){
if (dig[i]!==dig[NoD-1-i]) return false;
}
return true;
}
function largestPalindromeProduct(n) {
// Good luck!
let c = Math.pow(10,n);
let f = Math.pow(10,n-1);
//console.log(c);
let i, j, k;
let arrProduct = [];
k = 0;
for (i=f; i<c; i++){
for (j=f; j<=i; j++){
arrProduct[k] = i*j;
k = k+1;
}
}
arrProduct.sort((a,b)=>(b-a));
for (i=0; i<arrProduct.length; i++){
if (isPalin(arrProduct[i])) return arrProduct[i];
}
return true;
}
largestPalindromeProduct(3);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-4-largest-palindrome-product/