Skip to main content

Posts

Showing posts with the label Palindrome

Palindrome Checker回文检查

 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. 主要思路是,把字符串转换为一个数组,然里利用数组的reverse()方法,得到一个新的数组,这个新的数组再合成字符串,和原来的字符串进行比较。 function   palindrome ( str ) {    var   pattern  =  /[^a-z0-9A-Z]/ gi ;    var   newstr  =  str . replace ( pattern , "" ). toLowerCase ();    console . log ( newstr );    var   newarr  =  newstr . split ( "" ). reverse ();    console . log ( newarr );    var   reversedStr  =  newarr . join ( "" );    console . log ( reversedStr );    if  ( reversedStr == newstr )   {      return   true ;   }     return   false ; } palindrome ( "eye" );