Skip to content

Instantly share code, notes, and snippets.

@ninjascribble
Last active December 12, 2015 05:09
Show Gist options
  • Save ninjascribble/4719993 to your computer and use it in GitHub Desktop.
Save ninjascribble/4719993 to your computer and use it in GitHub Desktop.
Quick 'n dirty interview question #2
/**
* Given a list of words, write a function that returns only those
* words which are palindromes (written the same forwards and
* backwards).
*
* Ex.
*
* var list = ['racecar', 'balloon', 'moon', 'history'];
* findPalindromes(list) == ['racecar', 'moon'];
*/
var list = [
'racecar',
'sledgehammer',
'potato',
'kayak',
'rotator',
'renaissance'
];
function findPalindromes(list) {
var result = []
, i = 0
, len = list.length;
for (i; i < len; i++) {
// There are lots of ways to do this, but the basic idea
// is that you need to compare the string to the reverse
// of the string.
if (list[i].toLowerCase() === list[i].split('').reverse().join('').toLowerCase()) {
result.push(list[i]);
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment