-
-
Save anonymous/d6ebf2f217dc56048a47 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/twhite96 's solution for Bonfire: Check for Palindromes
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
// Bonfire: Check for Palindromes | |
// Author: @twhite96 | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-check-for-palindromes?solution=function%20palindrome(str)%20%7B%0A%20%20%2F%2F%20Good%20luck!%0A%20%20%0A%20%20str%20%3D%20str.toLowerCase()%3B%0A%20%20%0A%20%20str%20%3D%20str.replace(%2F%5B%5Ea-z%7C1-9%5D%2Fg%2C%20%22%22)%3B%0A%20%20%0A%20%20if%20(str.length%20%3D%3D%3D%200)%7B%0A%20%20%20%20return%20true%3B%0A%20%20%7D%0A%20%20%0A%20%20if%20(str%5B0%5D%20!%3D%3D%20str%5Bstr.length-1%5D)%7B%0A%20%20%20%20return%20false%3B%0A%20%20%7D%0A%20%20%0A%20%20else%20%7B%0A%20%20%20%20return%20palindrome(str.slice(1%2Cstr.length%20-%201))%3B%0A%20%20%7D%0A%7D%0A%0A%0Apalindrome(%22eye%22)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function palindrome(str) { | |
str = str.toLowerCase().replace(/[\W_]/g, ''); | |
for(var i = 0, len = str.length - 1; i < len/2; i++) { | |
if(str[i] !== str[len-i]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
palindrome("eye"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment