-
Notifications
You must be signed in to change notification settings - Fork 0
/
p04_palindromeProduct.js
62 lines (52 loc) · 1.6 KB
/
p04_palindromeProduct.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*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.
*/
/*
* isPalindrome takes in a number and checks whether
* it is a palindrome by converting it to a string and
* checking the characters one by one. Returns true or false.
*
* @param the number to check
*
* @return true/false
*/
function isPalindrome(num) {
var numString, lastIndex, index;
numString = num.toString();
lastIndex = numString.length - 1;
for (index = 0; index < numString.length / 2; index++) {
if (numString.charAt(index) !== numString.charAt(lastIndex - index)) {
return false;
}
}
return true;
}
/*
* Creating an array with every palindrome in the set of two 3-digit
* numbers multiplied together
*/
var outerIndex, innerIndex; //indices of the outer and inner for-loops
var maxVal, minVal; //max and min values to multiply together
var palindromeArray; //stores all palindromes
var testVal; //the value being tested with isPalindrome()
var largestPalindrome; //the largest palindrome
maxVal = 999;
minVal = 100;
palindromeArray = [];
for (outerIndex = maxVal; outerIndex >= minVal; outerIndex--) {
for (innerIndex = maxVal; innerIndex >= minVal; innerIndex--) {
testVal = outerIndex * innerIndex;
if (isPalindrome(testVal)) {
palindromeArray.push(testVal);
}
}
}
//Find the largest value in the array
largestPalindrome = palindromeArray.reduce(function (num1, num2) {
return Math.max(num1, num2);
});
console.log(largestPalindrome);