Last active
October 15, 2017 01:04
-
-
Save jialinhuang00/4af164106165bcf65322aefd65abecc3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/*----------------------------------------------------------------------------- | |
1.check if the string that reversed is as same as the origin. | |
2.ignoring the situation like upper or lower, and punctuation. | |
the reference used at the second function is from UdemyCourse: LearningAlgorithmsInJavascriptFromScratch | |
-----------------------------------------------------------------------------*/ | |
function isPalindrome(string) { | |
var reg = /[a-z]/g; | |
// change all to lower, focus on the alphabet, and put them together. | |
var normalStr = string.toLowerCase().match(reg).join(""); | |
// one more step than the normalStr: reverse() | |
var reversedStr = string.toLowerCase().match(reg).reverse().join(""); | |
return normalStr === reversedStr; | |
} | |
isPalindrome("aabbddsddbbaa"); | |
This file contains hidden or 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
function isPalindrome2(string) { | |
string = string.toLowerCase(); | |
var characterArr = string.split(""); | |
var validCharacters = "abcdefghijklmnopqrstuvwxyz".split(""); | |
var letterArr = []; | |
characterArr.forEach((char) => { | |
if (validCharacters.indexOf(char) > -1) { | |
letterArr.push(char); | |
} | |
}); | |
if (letterArr.join("") === letterArr.reverse().join("")) return true; | |
else return false; | |
} | |
isPalindrome2("aabbddsddbbaa"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment