Last active
December 12, 2015 05:09
-
-
Save ninjascribble/4719993 to your computer and use it in GitHub Desktop.
Quick 'n dirty interview question #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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