# Problem 4: Largest palindrome product help please

Hello campers ,please tell me what is happening here

``````function largestPalindromeProduct(n) {
// Good luck!
let num=1;
let Palind;
for (let i=0;i<n;i++){
num*=10;
}
let lenC;
for(let j=num-1;j>0;j--){
for(let c=num-1;c>0;c--){
Palind=j*c;
lenC=c.toString().length;

if(Palind===parseFloat(Palind.toString().split("").reverse().join("")) && lenC===n){
return console.log(Palind)
}
}
}
}

largestPalindromeProduct(3);
``````

My algorithm is for example if n = 2 so the first value is 99 /99 then 99/98 and so one
it countinues looping if I did not find the palindromic number 99 become 98 then 98/99 98/98 98/97 so on so fourth but his algorithm is not working with n =3
largestPalindromeProduct(2)`should return 9009 // this one is correct largestPalindromeProduct(3)` should return 90660 // this one is not
https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-4-largest-palindrome-product/

OK, a couple of issues here:

My algorithm is for example if n = 2 so the first value is 99

Good logic. Instead of looping and multiplying by 10, you could have used `Math.pow` or the new `**` operator, but it still gets the right number.

99 /99 then 99/98 and so one
it countinues looping if I did not find the palindromic number 99 become 98 then 98/99 98/98 98/97 so on so fourth

OK, you are misunderstanding how nested loops work. Put `console.log('j =', j, 'and c =', c);` inside the inner most loop and see. It’s going to go 99/99, 98/99, 97/99, 96/99 … all the way to 1/99 and only then get to 98/99, 97/99… There is no guarantee that the first palindrome number you hit with this method will be the largest.

So, there are two options, switch how you loop the numbers or change your logic of how you evaluate and return the largest palindrome. They are both worth exploring but I will address the latter because it is easiest with your code.

Inside the inner loop you have:

``````Palind = j * c;
lenC = c.toString().length;
if (Palind === parseFloat(Palind.toString().split("").reverse().join("")) && lenC === n) {
return console.log(Palind);
}
``````

First of all, why are you checking the length of your product? The length of `n` requirement refers to the multiplicands, not the product.

In any case, instead of returning the first palindrome you find, you need to see if that is bigger than the biggest palindrome you’ve found so far and store it. You don’t return the answer until you’ve finished all the checks.

A few thoughts on JS conventions:

Usually we don’t use Pascal case (camel case with a starting capital) except for classes, constructors, or React components.

Secondly, what are the `j` and `c` variables named for? Those seem like odd choices. We usually don’t use on character names unless it is a common indexing variable (like `i`, `j`, and then, `k`) or if it is a parameter in a short callback function. It should at least be instantly obvious. By convention, I would have gone with `i` and `j`.

Also, `return console.log(Palind)` isn’t going to work because afaik ‘console.log’ returns `undefined`, not what was passed to it. But this might have been accidentally left over from debugging.

Keep at it. Let us know if you need more help.

1 Like

Now that I think about it, I’m not sure the first fix I hinted at (“switch how you loop the numbers”) would even work. I’m not sure that would be possible to check that way because it is assuming that the largest palindromic product would be generated by adjacent multiplicands, which is not true. Two other options come to mind that comes to mind:

• Generate all the possible products, sort them and start from the top until you find a palindrome.
• Generate all the palindromes with the correct number of digits and start from the top, finding the first with at least two n-digit factors.

Those sound messy. I like what we’re doing.

1 Like

Thank you for your help and your time , I reread the challenge , it seems like I am understanding what I should solve , when you said to me Put `console.log('j =', j, 'and c =', c)` in the most inner loop the result that I got is

``````
j = 99 and c = 99
j = 99 and c = 98
j = 99 and c = 97
j = 99 and c = 96
j = 99 and c = 95
j = 99 and c = 94
j = 99 and c = 93
j = 99 and c = 92
j = 99 and c = 91

9009
``````

this result is implemented if n=2 so 9009 is the palindrome and it is the largest
theres is no number larger than 99*91 and it gives a palindrome number right .
but if n =3
the result becomes

``````j = 999 and c = 999
j = 999 and c = 998
j = 999 and c = 997
j = 999 and c = 996
``````

it continues looping untill it gets
a palindrome wich is this one 90909 wich is made by these integers j = 999 and c = 91
and this is why I used lenC = c.toString().length; to not allow the palindrome to have just five numbers
Nevertheless ,my thought somehowe is wrong ,something happened ,when I added lenC===n
it loops as I mentioned above but it doesn’t continue looping it jumpes straight away to this palindrome which is not correct 580085
at the first time when I was thinking about this ,I said this is gonna give an infinite loop
but that was wrong so my question is if it found 580085 as palindrome
why it has not found 906609 which is largest ,because when the loop goes down ,it will hit it .
I would like to change this logic and think about andother wayout but I am still confusing about this one , why it is not working as I planned.

this result is implemented if n=2 so 9009 is the palindrome and it is the largest theres is no number larger than 99*91 and it gives a palindrome number right

Yes, your solution will work for n=1 and n=2. But that is just coincidence. For example the algorithms `n+n`, `n*2`, and `n**2` will all give the same result for n=0 and n=2. But that does not “prove” that those formulas are interchangeable. Don’t assume that because an algorithm works for a few test cases that it is correct. You’re algorithm is incorrect for the reasons I explained above. You are looking for the first palindrome you can find and assuming that that is the largest one. There is no mathematical basis for that assumption.

… a palindrome wich is this one 90909 wich is made by these integers j = 999 and c = 91 and this is why I used lenC = c.toString().length;

IC. I get what you’re going for. Of course, you’re only checking one of the numbers. Perhaps you’re assuming that you’ll find a palindrome before j drops below n digits. That is probably a reasonable assumption.

But I think the instruction “Find the largest palindrome made from the product of two `n`-digit numbers.” is misworded.

1. It is probably safe to assume that there will always be a palindrome that is the product of two n-digit numbers so this is a meaningless test. When I get back from my run, I will test this theory.
2. They probably meant to say “…product of two `n`-digit or less numbers.” My “evidence”? No competent programmer would right out instructions for something like this, where s/he expected that there would be cases where there wouldn’t be two multiplicands that were exactly `n`-digits and not tell you what to return in that case. Do I return `null`? `Undefined`? `0`? `-1`? An error message? Do I throw and exception? Do I return the largest palindrome I did find? Essentially the way my code works is if there isn’t a palindrome the product of two `n`-digit multiplicands, then it returns the largest one it does find, and if it finds none, it returns `undefined`. Without clarification, that is a reasonable assumption.

But I just think it’s misworded. This is just copied from Project Euler so I’ll check for them and I’m going to look around online and see what I can see.

In the meantime, don’t worry about it. Forget about that condition. Just find the largest possible palindrome for “`n`-digits” or less and you’ll have your answer.

If you really, really want to deal with this, then I think controlling `for` loop is a better way to go. You are setting num to `(10**n)-1`, as the start of your loops, you can also set the end of the lop at `10**(n-1)` and you’ll never test any “wrong” digit numbers.

1 Like

Thank you for this clarification , I will try to fix this later,sometimes a solution of something comes in mind when I am about sleeping or outside walking around my city.
as you said this challnege is misleading it s not clear .

Thanks man I got the solution , with pushing every palind to an(arr) and at the end
I used Math.max(…arr) .

Cool, good job. I just might suggest that instead of keeping ever palindrome and then sorting through to find the max, it might be easier just to keep track of the biggest so far. It does the same thing, but would be slightly more efficient. Part of learning algorithms is learning to think this way.

``````  let num=Math.pow(10,n);
let result=0;
let palind;
for(let j=num-1;j>0;j--){
for(let c=num-1;c>0;c--){
palind=j*c;
if(palind===parseFloat(palind.toString().split("").reverse().join(""))){
if(palind > result){
result=palind;
}
}
}
}
return console.log(result);
``````

here it is without using Mat.max() method .

Cool, good job. Just as a reminder, please don’t post the solutions to the problems without blurring them - put then inside spoiler tags (`[spoiler]` and `[/spoiler]`).

Have fun on the next challenge. Don’t get frustrated - some of these are hard.

1 Like