2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: aaa48de84e1ecc7c742e1124
|
|
|
|
|
challengeType: 5
|
2020-09-07 16:17:22 +08:00
|
|
|
|
forumTopicId: 16004
|
|
|
|
|
localeTitle: 回文检查器
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-09-07 16:17:22 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
如果给定的一个字符串是回文,那么返回<code>true</code>,否则返回<code>false</code>。
|
|
|
|
|
<dfn>palindrome(回文)</dfn>,指在忽略标点符号、大小写和空格的前提下,正着读和反着读一模一样。
|
|
|
|
|
<strong>注意:</strong><br>检查回文时,你需要先除去<strong>所有非字母数字的字符</strong>(标点、空格和符号)并且将所有字符转换成字母大写或字母小写。
|
|
|
|
|
我们将会传入不同格式的字符串,例如:<code>"racecar"</code>、<code>"RaceCar"</code>、<code>"race CAR"</code>等等。
|
|
|
|
|
我们也会传入一些包含特殊符号的字符串,例如<code>"2A3*3a2"</code>,<code>"2A3 3a2"</code>和<code>"2_A3*3#A2"</code>。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-09-07 16:17:22 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('eye')</code>应该返回一个布尔值。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(typeof palindrome("eye") === "boolean");
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('eye')</code>应该返回 true。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("eye") === true);
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('_eye')</code>应该返回 true。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("_eye") === true);
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('race car')</code>应该返回 true。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("race car") === true);
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('not a palindrome')</code>应该返回 false。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("not a palindrome") === false);
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('A man, a plan, a canal. Panama')</code>应该返回 true。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("A man, a plan, a canal. Panama") === true);
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('never odd or even')</code>应该返回 true。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("never odd or even") === true);
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('nope')</code>应该返回 false。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("nope") === false);
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('almostomla')</code>应该返回 false。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("almostomla") === false);
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('My age is 0, 0 si ega ym.')</code>应该返回 true。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("My age is 0, 0 si ega ym.") === true);
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('1 eye for of 1 eye.')</code>应该返回 false。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("1 eye for of 1 eye.") === false);
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: '<code>palindrome("0_0 (: /-\ :) 0-0")</code>应该返回 true。'
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: 'assert(palindrome("0_0 (: /-\ :) 0-0") === true);'
|
2020-09-07 16:17:22 +08:00
|
|
|
|
- text: "<code>palindrome('five|\_/|four')</code>应该返回 false。"
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(palindrome("five|\_/|four") === false);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function palindrome(str) {
|
|
|
|
|
// Good luck!
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
palindrome("eye");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
2020-09-07 16:17:22 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-09-07 16:17:22 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2020-09-07 16:17:22 +08:00
|
|
|
|
</section>
|