Skip to content

Instantly share code, notes, and snippets.

@Elsayegh
Created September 1, 2018 11:10
Show Gist options
  • Select an option

  • Save Elsayegh/ab6c10441e60a45b57c8ff8d154a8139 to your computer and use it in GitHub Desktop.

Select an option

Save Elsayegh/ab6c10441e60a45b57c8ff8d154a8139 to your computer and use it in GitHub Desktop.
FreeCodeCamp JavaScript Algorithms and Data Structures Projects
*/Return true if the given string is a palindrome. Otherwise, return false.
A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
Note
You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the same case
(lower or upper case) in order to check for palindromes.
We'll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others.
We'll also pass strings with special symbols, such as "2A3*3a2", "2A3 3a2", and "2_A3*3#A2".
*/
function palindrome(str) {
// Good luck!
var exp = /[\W_]/g; //Remove all non-alphanumeric characters
var lowerCaseStr = str.toLowerCase().replace(exp, ''); //Lower case the string
var backWardsStr = lowerCaseStr.split('').reverse().join(''); //treat the string as an array ,
Reverse it and then unite the string together again
//if(str === backWardsStr)
return lowerCaseStr === backWardsStr;
}
palindrome("eye");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment