Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Made Code Dojo 4: Palindromic Numbers
SUCCESS
## http://projecteuler.net/problem=4
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.
function Palindrome () { };
Palindrome.prototype.isPalindrome = function (candidate) {
reversedCandidate = candidate.toString().split("").reverse().join("");
return candidate.toString() == reversedCandidate;
};
Palindrome.prototype.product = function (number1, number2) {
return number1 * number2;
};
Palindrome.prototype.highestNumberForNumberOfDigits = function (numberOfDigits) {
return Math.pow(10, numberOfDigits) - 1;
};
Palindrome.prototype.lowestNumberForNumberOfDigits = function (numberOfDigits) {
return Math.pow(10, numberOfDigits - 1);
};
Palindrome.prototype.highestProduct = function (numberOfDigits) {
var highestNumber = this.highestNumberForNumberOfDigits(numberOfDigits),
lowestNumber = this.lowestNumberForNumberOfDigits(numberOfDigits),
digit1 = highestNumber,
digit2 = highestNumber,
product,
highestPalindrome = 0;
for (; digit1 >= lowestNumber; digit1--) {
for (; digit2 >= lowestNumber; digit2--) {
product = this.product(digit1, digit2);
if (this.isPalindrome(product) && (product > highestPalindrome)) {
highestPalindrome = product;
}
}
digit2 = highestNumber;
};
return highestPalindrome;
};
Palindrome.prototype.highestProductFor2Digits = function () {
return this.highestProduct(2);
};
Palindrome.prototype.highestProductFor3Digits = function () {
return this.highestProduct(3);
};
describe('Palindrome', function () {
var palindrome = new Palindrome();
describe('isPalindrome', function () {
it('should return false for a non palindrome', function () {
expect(palindrome.isPalindrome(54)).toBe(false);
});
it('should return true for a palindrome', function () {
expect(palindrome.isPalindrome(9009)).toBe(true);
});
});
describe('product', function () {
it('should return the product of 2 numbers', function () {
expect(palindrome.product(10, 10)).toBe(100);
});
});
describe('highestProductFor2Digits', function () {
it('should return the highest product of 2 digit numbers that is a palindrome', function () {
expect(palindrome.highestProductFor2Digits()).toBe(9009);
});
});
describe('highestProductFor3Digits', function () {
it('should return the highest product of 3 digit numbers that is a palindrome', function () {
expect(palindrome.highestProductFor3Digits()).toBe(906609);
});
});
describe('highestNumberForNumberOfDigits', function () {
it('should return highest number given number of digits', function () {
expect(palindrome.highestNumberForNumberOfDigits(3)).toBe(999);
});
});
describe('lowestNumberForNumberOfDigits', function () {
it('should return lowest number given number of digits', function () {
expect(palindrome.lowestNumberForNumberOfDigits(3)).toBe(100);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment