--- id: aaa48de84e1ecc7c742e1124 challengeType: 5 forumTopicId: 16004 localeTitle: 回文检查器 --- ## Description
如果给定的一个字符串是回文,那么返回true,否则返回falsepalindrome(回文),指在忽略标点符号、大小写和空格的前提下,正着读和反着读一模一样。 注意:
检查回文时,你需要先除去所有非字母数字的字符(标点、空格和符号)并且将所有字符转换成字母大写或字母小写。 我们将会传入不同格式的字符串,例如:"racecar""RaceCar""race CAR"等等。 我们也会传入一些包含特殊符号的字符串,例如"2A3*3a2""2A3 3a2""2_A3*3#A2"
## Instructions
## Tests
```yml tests: - text: "palindrome('eye')应该返回一个布尔值。" testString: assert(typeof palindrome("eye") === "boolean"); - text: "palindrome('eye')应该返回 true。" testString: assert(palindrome("eye") === true); - text: "palindrome('_eye')应该返回 true。" testString: assert(palindrome("_eye") === true); - text: "palindrome('race car')应该返回 true。" testString: assert(palindrome("race car") === true); - text: "palindrome('not a palindrome')应该返回 false。" testString: assert(palindrome("not a palindrome") === false); - text: "palindrome('A man, a plan, a canal. Panama')应该返回 true。" testString: assert(palindrome("A man, a plan, a canal. Panama") === true); - text: "palindrome('never odd or even')应该返回 true。" testString: assert(palindrome("never odd or even") === true); - text: "palindrome('nope')应该返回 false。" testString: assert(palindrome("nope") === false); - text: "palindrome('almostomla')应该返回 false。" testString: assert(palindrome("almostomla") === false); - text: "palindrome('My age is 0, 0 si ega ym.')应该返回 true。" testString: assert(palindrome("My age is 0, 0 si ega ym.") === true); - text: "palindrome('1 eye for of 1 eye.')应该返回 false。" testString: assert(palindrome("1 eye for of 1 eye.") === false); - text: 'palindrome("0_0 (: /-\ :) 0-0")应该返回 true。' testString: 'assert(palindrome("0_0 (: /-\ :) 0-0") === true);' - text: "palindrome('five|\_/|four')应该返回 false。" testString: assert(palindrome("five|\_/|four") === false); ```
## Challenge Seed
```js function palindrome(str) { // Good luck! return true; } palindrome("eye"); ```
## Solution
```js function palindrome(str) { var string = str.toLowerCase().split(/[^A-Za-z0-9]/gi).join(''); var aux = string.split(''); if (aux.join('') === aux.reverse().join('')){ return true; } return false; } ```