Find the largest palindrome made from the product of two 3-digit numbers - Javascript

前端 未结 21 1429
感情败类
感情败类 2020-12-28 17:16

Can anyone tell me what\'s wrong with the code. Find the largest palindrome made from the product of two 3-digit numbers.

function largestPalind         


        
21条回答
  •  [愿得一人]
    2020-12-28 17:30

    This is how I did it in Javascript. Simple & easy!

    let num1 = 999;
    let num2 = 999;
    let arr = [];
    
    function check(x, y)
    {
        if(String(x*y) == String(x*y).split("").reverse().join(""))
        {
            return true;
        }
    return false;
    }
    
    for(let i=0; i<999999; i++)
    {
        if(check(num1, num2))
        {
            arr.push(num1*num2);
            num1--;
            num2 = num1+1;
        }
    num2--;
    }
    
    console.log(arr.sort((x, y) => y-x)[0]);
    

提交回复
热议问题