加州提子面包

JavaScript算法试题系列-05

题目要求:

1
2
// 会问字符串判断某个字符串是否为回文字符串,譬如racecar与race car都是回文字符串:isPalindrome("racecar"); // true
isPalindrome("race Car"); // true

解答:

1
2
3
4
5
6
7
function isPalindrome(word) {
// Replace all non-letter chars with "" and change to lowercase
var lettersOnly = word.toLowerCase().replace(/\s/g, "");
// Compare the string with the reversed version of the string
return lettersOnly === lettersOnly.split("").reverse().join("");
}
------ 本文结束 ------