Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 8536bdf

Browse files
committed
Palindrome for your Dome
1 parent b739acb commit 8536bdf

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#Description
2+
3+
A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Famous examples include "Amore, Roma", "A man, a plan, a canal: Panama" and "No 'x' in 'Nixon'". - wikipedia
4+
5+
Our goal is to determine whether or not a given string is a valid palindrome or not.
6+
7+
Like the above examples, here are a few test cases which are also populated:
8+
9+
```
10+
"Amore, Roma" => valid
11+
"A man, a plan, a canal: Panama" => valid
12+
"No 'x' in 'Nixon'" => valid
13+
"Abba Zabba, you're my only friend" => invalid
14+
```
15+
16+
You can see that they are case insensitive and disregards non alphanumeric characters. In addition to a few predefined tests, your function will also be tested against a random string generator 50 times which are guaranteed to produce valid palindromes.
17+
18+
NOTE: reverse/reverse! have been disabled for String/Array and reverse() for JS.
19+
20+
#Best practice
21+
22+
**First:**
23+
```
24+
function palindrome(string) {
25+
var sanitized = string.replace(/[^A-Za-z]/g, "").toLowerCase();
26+
return sanitized == sanitized.split("").reduceRight(function(sum, v) {return sum + v;});
27+
}
28+
```
29+
30+
**Second:**
31+
```
32+
function palindrome(string) {
33+
var s = string.replace(/[^A-Za-z0-9]/g, "").toLowerCase();
34+
for (var i = 0; i < s.length/2; i++) if (s[i] != s[s.length-i-1]) return false;
35+
return true;
36+
}
37+
```
38+
39+
**Third:**
40+
```
41+
function palindrome(string) {
42+
var s = string.toLowerCase().replace(/[^a-z0-9]+/g, '');
43+
return s == s.split('').reduce(function(str, value) {
44+
return value+str;
45+
}, '');
46+
}
47+
```
48+
49+
**Fourth:**
50+
```
51+
function palindrome(string) {
52+
return string.toLowerCase().replace(/[^a-z]/gi,'').split('').every(function(a,b,c){
53+
return a===c[c.length-b-1]
54+
})
55+
}
56+
```
57+
58+
**Fifth:**
59+
```
60+
function palindrome(string) {
61+
return string
62+
.toLowerCase()
63+
.replace(/[^a-z]/g,'')
64+
.split("")
65+
.every(function(v, i, array){ return v == array[array.length-i-1] })
66+
;
67+
}
68+
```
69+
70+
Kata's link: [Palindrome for your Dome](http://www.codewars.com/kata/palindrome-for-your-dome/)

summary.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
[Sum of many ints](codewars/sum-of-many-ints.md);
1+
[Sum of many ints](codewars/sum-of-many-ints.md)
2+
[Palindrome for your Dome](codewars/palindrome-for-your-dome.md)

0 commit comments

Comments
 (0)