Skip to content

Instantly share code, notes, and snippets.

@felixcatto
Last active September 7, 2016 07:09
Show Gist options
  • Save felixcatto/f9f498360bd35d99f51fd4a53166b0e5 to your computer and use it in GitHub Desktop.
Save felixcatto/f9f498360bd35d99f51fd4a53166b0e5 to your computer and use it in GitHub Desktop.
String.prototype.generateSubstrings = function() {
var string = this;
var stringLen = string.length;
var innerIterWtf = function(substrings, i, j) {
if (j == stringLen)
return substrings
else {
var substring = string.slice(i, j+1);
substrings.push(substring);
return innerIterWtf(substrings, i, j+1);
}
};
var iter = function(substrings, i) {
if (i == stringLen-1)
return substrings;
else {
innerIterWtf(substrings, i, i+1);
return iter(substrings, i+1);
}
};
return iter([], 0);
}
String.prototype.isPalindrome = function() {
return this == this.split('').reverse().join('');;
}
Array.prototype.sortByLengthDesc = function() {
var array = this;
var compareByLength = function(a, b) {
if (a.length > b.length)
return -1;
else if (a.length < b.length)
return 1;
else
return 0;
};
array.sort(compareByLength);
return array;
}
Array.prototype.getFirst = function() {
return this[0];
}
'xyzzy'
.generateSubstrings()
.filter(function(el){
return el.isPalindrome();
})
.sortByLengthDesc()
.getFirst()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment