var input = 'FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth' ;
function start( text) {
var palindromes = [ ] , letter, i, answer;
var add = function ( palindrome) {
palindromes.push ( palindrome) ;
} ;
// Loop through each letter of the alphabet
for ( i = 97 ; i < 123 ; i += 1 ) {
letter = String .fromCharCode ( i) ;
getSequences( text, getPositions( text, letter) ) .forEach ( add) ;
}
// Sort in descending order from length and pick the first one
answer = palindromes.sort ( function ( a, b) {
return ( a.length > b.length ? - 1 : 1 ) ;
} ) [ 0 ] ;
console.log ( 'The longest palindrome is: ' + answer) ;
}
// Gets positions of specific character
function getPositions( text, character) {
var positions = [ ] ;
text.split ( '' ) .forEach ( function ( c, pos) {
if ( c === character) {
positions.push ( pos) ;
}
} ) ;
return positions;
}
// Gets strings from each different position pair for each letter
function getSequences( text, positions) {
var i, j, sequences = [ ] , current;
for ( i = 0 ; i < positions.length ; i += 1 ) {
for ( j = 0 ; j < positions.length ; j += 1 ) {
current = text.substring ( positions[ i] , positions[ j] + 1 ) ;
if ( current.split ( '' ) .reverse ( ) .join ( '' ) === current && current.length > 1 ) {
sequences.push ( current) ;
}
}
}
return sequences;
}
start( input) ;
dmFyIGlucHV0ID0gJ0ZvdXJzY29yZWFuZHNldmVueWVhcnNhZ29vdXJmYWF0aGVyc2Jyb3VnaHRmb3J0aG9udGhpc2NvbnRhaW5lbnRhbmV3bmF0aW9uY29uY2VpdmVkaW56TGliZXJ0eWFuZGRlZGljYXRlZHRvdGhlcHJvcG9zaXRpb250aGF0YWxsbWVuYXJlY3JlYXRlZGVxdWFsTm93d2VhcmVlbmdhZ2VkaW5hZ3JlYWh0Y2l2aWx3YXJ0ZXN0aW5nd2hldGhlcnRoYXRuYXB0aW9ub3JhbnluYXJ0aW9uc29jb25jZWl2ZWRhbmRzb2RlZGljYXRlZGNhbmxvbmdlbmR1cmVXZWFyZXFtZXRvbmFncmVhdGJhdHRsZWZpZW1sZG9mdHpoYXR3YXJXZWhhdmVjb21ldG9kZWRpY3BhdGVhcG9ydGlvbm9mdGhhdGZpZWxkYXNhZmluYWxyZXN0aW5ncGxhY2Vmb3J0aG9zZXdob2hlcmVnYXZldGhlaXJsaXZlc3RoYXR0aGF0bmF0aW9ubWlnaHRsaXZlSXRpc2FsdG9nZXRoZXJmYW5nYW5kcHJvcGVydGhhdHdlc2hvdWxkZG90aGlzQnV0aW5hbGFyZ2Vyc2Vuc2V3ZWNhbm5vdGRlZGljYXRld2VjYW5ub3Rjb25zZWNyYXRld2VjYW5ub3RoYWxsb3d0aGlzZ3JvdW5kVGhlYnJhdmVsbWVubGl2aW5nYW5kZGVhZHdob3N0cnVnZ2xlZGhlcmVoYXZlY29uc2VjcmF0ZWRpdGZhcmFib3Zlb3VycG9vcnBvbndlcnRvYWRkb3JkZXRyYWN0VGdoZXdvcmxkYWRzd2ZpbGxsaXR0bGVub3RsZW5vcmxvbmdyZW1lbWJlcndoYXR3ZXNheWhlcmVidXRp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ICAgICAgICAgICAgICAgICAgICAgICAgICBsZXR0ZXIgPSBTdHJpbmcuZnJvbUNoYXJDb2RlKGkpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGdldFNlcXVlbmNlcyh0ZXh0LCBnZXRQb3NpdGlvbnModGV4dCwgbGV0dGVyKSkuZm9yRWFjaChhZGQpOwogICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIC8vIFNvcnQgaW4gZGVzY2VuZGluZyBvcmRlciBmcm9tIGxlbmd0aCBhbmQgcGljayB0aGUgZmlyc3Qgb25lCiAgICAgICAgICAgICAgICBhbnN3ZXIgPSBwYWxpbmRyb21lcy5zb3J0KGZ1bmN0aW9uKGEsIGIpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gKGEubGVuZ3RoID4gYi5sZW5ndGggPyAtMSA6IDEpOwogICAgICAgICAgICAgICAgfSlbMF07CgogICAgICAgICAgICAgICAgY29uc29sZS5sb2coJ1RoZSBsb25nZXN0IHBhbGluZHJvbWUgaXM6ICcgKyBhbnN3ZXIpOwp9CgovLyBHZXRzIHBvc2l0aW9ucyBvZiBzcGVjaWZpYyBjaGFyYWN0ZXIKZnVuY3Rpb24gZ2V0UG9zaXRpb25zKHRleHQsIGNoYXJhY3RlcikgewogICAgICAgICAgICAgICAgdmFyIHBvc2l0aW9ucyA9IFtdOwoKICAgICAgICAgICAgICAgIHRleHQuc3BsaXQoJycpLmZvckVhY2goZnVuY3Rpb24oYywgcG9zKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYoYyA9PT0gY2hhcmFjdGVyKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBvc2l0aW9ucy5wdXNoKHBvcyk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSk7CgogICAgICAgICAgICAgICAgcmV0dXJuIHBvc2l0aW9uczsKfQoKLy8gR2V0cyBzdHJpbmdzIGZyb20gZWFjaCBkaWZmZXJlbnQgcG9zaXRpb24gcGFpciBmb3IgZWFjaCBsZXR0ZXIKZnVuY3Rpb24gZ2V0U2VxdWVuY2VzKHRleHQsIHBvc2l0aW9ucykgewogICAgICAgICAgICAgICAgdmFyIGksIGosIHNlcXVlbmNlcyA9IFtdLCBjdXJyZW50OwoKICAgICAgICAgICAgICAgIGZvcihpID0gMDsgaSA8IHBvc2l0aW9ucy5sZW5ndGg7IGkgKz0gMSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZvcihqID0gMDsgaiA8IHBvc2l0aW9ucy5sZW5ndGg7IGogKz0gMSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjdXJyZW50ID0gdGV4dC5zdWJzdHJpbmcocG9zaXRpb25zW2ldLCBwb3NpdGlvbnNbal0gKyAxKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYoY3VycmVudC5zcGxpdCgnJykucmV2ZXJzZSgpLmpvaW4oJycpID09PSBjdXJyZW50ICYmIGN1cnJlbnQubGVuZ3RoID4gMSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc2VxdWVuY2VzLnB1c2goY3VycmVudCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgcmV0dXJuIHNlcXVlbmNlczsKfQoKc3RhcnQoaW5wdXQpOw==