-
Notifications
You must be signed in to change notification settings - Fork 1
/
004-largest_palindrome_product.js
47 lines (37 loc) · 1.24 KB
/
004-largest_palindrome_product.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Checks if a given String is a Palindrome */
let isPalindrome = (string) => {
let backwardString = string.split('').reverse().join('')
if(string === backwardString){
return true
}
return false
}
/* Solution Function */
let largestPalindromeProduct = (numberOfdigits) => {
/* Generates Array of Numbers Available to Multiply */
let arrayOfNumbers = []
let current = 0
while(!(current.toString().length > numberOfdigits)){
if(current.toString().length === numberOfdigits){
arrayOfNumbers.push(current)
}
current = current + 1
}
let largestProduct = 0
let i
let j
/* Iterate through Array and Multiply all Numbers with each other */
for(i = 0; i < arrayOfNumbers.length; i ++){
for(j = i; j < arrayOfNumbers.length; j ++){
let product = arrayOfNumbers[i] * arrayOfNumbers[j]
// If product is a palindrome and larger than the largest
// product discovered so far, update it
if(isPalindrome(product.toString()) && product > largestProduct){
largestProduct = product
}
}
}
return largestProduct
}
/* Check Solution */
console.log(largestPalindromeProduct(3))