Skip to content

Instantly share code, notes, and snippets.

@christopherhill
Created December 31, 2015 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopherhill/07cd6f7c34d11f506bff to your computer and use it in GitHub Desktop.
Save christopherhill/07cd6f7c34d11f506bff to your computer and use it in GitHub Desktop.
// palindrome
var words = ['otto', 'hannah', 'schlumberger'];
var test = function(words) {
for (var i = 0; i < words.length; i++) {
var result = palindrome(words[i]);
console.log(result);
}
};
var palindrome = function(word) {
var results = [];
for (var i = 0; i <= word.length; i++) {
for (var j = 0; j <= word.length; j++) {
var slice = word.slice(i, j);
if (slice.length > 1) {
var isPalindrome = (slice === slice.split('').reverse().join(''));
if (isPalindrome) {
results.push(slice);
}
}
}
}
return results;
};
test(words);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment