HELP: Problem 4: Largest palindrome product

HELP: Problem 4: Largest palindrome product
0

#1

Tell us what’s happening:
I test both case and it shows 9009 and 906609 in my console.
But I cant pass the test :frowning:

Your code so far


function largestPalindromeProduct(digit) {
  
  var maxpalindrome = 0;
  var factor = 0;

  for(let i = Math.pow(10, digit-1); i<Math.pow(10,digit); i++){

    var num = ''+i;
    var newnum = num;
    for (let j = num.length; j>0; j--){
      newnum += num.charAt(j-1);
    }
    
    for(let k = Math.pow(10, digit-1); k<Math.pow(10,digit); k++){
      factor = (Number(newnum))/k;
      if((factor%1 === 0) && String(factor).length == digit){
        maxpalindrome = newnum;
      }
    }
 
  }
  
  return console.log(maxpalindrome);
} 

largestPalindromeProduct(2);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-4-largest-palindrome-product/


#2

I copy/pasted your code into the challenge, nothing passed - one problem I see is that you are returning a console.log, you should just be returning the maxpalindrome variable - another thing I see is that your variable is a string, you should change it to an integer before you return it - after doing these two things, the first test passed - the code does seem to produce the correct output for the last test in codepen for me, but in the fcc editor it doesn’t - what’s happening there is that the test is timing out, meaning there’s a safety feature in the tests to stop infinite loops if it takes too long - it is described here Free Code Camp Infinite Loop Protection - the //noprotect workaround doesn’t work at the moment since the new site came live, I’m not sure of any other way to get around that other than making your code faster, which isn’t necessary since it seems to work