# Project Euler: #4 - Largest palindrome product

### **Problem**

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

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

---

### **Problem Description**

A palindromic number reads the same both forward and also backward.

One such palindromic number is `9009` which is the largest palindrome made by multiplying two 2-digit numbers, `91 * 99`

We need to find the largest Palindrome that can be made by multiplying two 3-digit numbers

---

### **Approach**

The smallest 3-digit number is `100` and the largest is `999`. Since we need the largest palindrome, we approach this from the largest to the smallest, `999` -&gt; `100`.

Two reversed `for` loops starting from `999` for each number, eg: `99 * 99`, `99 * 98`, `99 * 97` etc

To check if the product is Palindrome,

* convert the number to `string`,
    
* then `split` it into an array,
    
* `reverse` the array &
    
* `join` the items to get the reversed number in `string` format.
    
* Now convert the reversed number to a `number` format and compare with the original number.
    
* If both are equal, then it is a palindrome.
    

---

### **Solution**

```javascript
const checkPalindrome = (number) => {
    let reverse = String(number)
        .split('')
        .reverse()
        .join('');
      return Number(reverse) === number;
}

const retLargestPalindrome = () => {
    let maxVal = 0;

    for (let i = 999; i >= 100; i--) {
        for (let j = 999; j >= 100; j--) {
            let prod = i * j;
            if (checkPalindrome(prod)) {
                if (prod > maxVal) maxVal = prod;
                // Can also use maxVal = Math.max(maxVal, prod);
                break;
            }
        }
    }
    return maxVal;
}

console.log(retLargestPalindrome());
```

My Solution is found in [04](https://github.com/sansk/project-euler-solutions-javascript/tree/main/04)

If you have another or a better solution, please leave it in the comments below.

For the other Project Euler Solutions, please follow the series [**Project Euler Solutions in JS.**](https://theintrovertcoder.hashnode.dev/series/project-euler)

Thank you!
