Skip to content

Instantly share code, notes, and snippets.

  • Save twhite96/a066072cd9194d249caa to your computer and use it in GitHub Desktop.
Save twhite96/a066072cd9194d249caa to your computer and use it in GitHub Desktop.
Bonfire: Check for Palindromes
// 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