Skip to content

Instantly share code, notes, and snippets.

@SerhiiLihus
Created November 24, 2015 08:01
Show Gist options
  • Save SerhiiLihus/97693640c9611701593c to your computer and use it in GitHub Desktop.
Save SerhiiLihus/97693640c9611701593c to your computer and use it in GitHub Desktop.
Check for Palindromes
// Bonfire: Check for Palindromes
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-check-for-palindromes?solution=function%20palindrome(str)%20%7B%0A%20%20var%20flag%20%3D%20true%3B%0A%20%20str%20%3D%20str.toLowerCase().replace(%2F%5B)(%3A%5C%5C%2F%5Cs.%2C_-%5D%2Fg%2C%27%27)%3B%0A%20%20for%20(var%20i%20%3D%200%20%3B%20%20i%20%3C%20Math.floor(str.length%20%2F%202)%20%3B%20i%2B%2B%20)%0A%20%20if%20(%20str.charAt(i)%20%3D%3D%3D%20str.charAt(%20str.length%20-%20i%20-%201%20)%20)%7B%0A%20%20%20%20%20%20flag%20%3D%20true%3B%0A%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20flag%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20break%3B%0A%20%20%7D%0A%20return%20flag%3B%0A%7D%0A%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function palindrome(str) {
var flag = true;
str = str.toLowerCase().replace(/[)(:\\/\s.,_-]/g,'');
for (var i = 0 ; i < Math.floor(str.length / 2) ; i++ )
if ( str.charAt(i) === str.charAt( str.length - i - 1 ) ){
flag = true;
} else {
flag = false;
break;
}
return flag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment