loading...

Code Golf Challenge: Palindrome Detector

daveturissini profile image David ・1 min read

Challenge: Using Javascript, implement a method that detects if a string is a palindrome in as few characters as possible!

The goal of this exercise is to write as little code as possible. Readability and maintainability are not a concern!

Solutions in comments πŸ‘‡πŸ‘‡

Discussion

pic
Editor guide
Collapse
quickhand profile image
quickhand

Short:

let p=s=>![...s].some((c,i,a)=>c!=a[a.length-i-1])

Shorter:

let p=s=>[...s].reverse().join("")==s

In both:

p("potato") //false
p("racecar") //true

(First works on palindromic arrays of characters as well as strings, second only on strings)

Collapse
daveturissini profile image
David Author

Nice. Really like the approach with β€œsome” in your first example

Collapse
shijiezhou profile image
Jay Chow
function palindrome(str) {
    var splitString = str.split("");
    var halfSplitString = splitString.length / 2;
    for (var i = 0; i < halfSplitString; i++) {
        if (str[i] !== str[str.length - i - 1]) {
            return false;
        }
        else {
            return true;
        }
    }
}
Collapse
daveturissini profile image
David Author

Nice! You can make the function even smaller if you just returned



Array.from(word)
.reverse()
.join("") === word
Collapse
wheatup profile image
Hao

Even shorter

word.split('').reverse().join('')==word
Thread Thread
nataliedeweerd profile image
𝐍𝐚𝐭𝐚π₯𝐒𝐞 𝐝𝐞 π–πžπžπ«π

This wouldn't work for "Arara" would it? It'd need some sort of case normalisation.

Also, what about "Taco cat".

Thread Thread
quickhand profile image
quickhand

The issuer of the challenge, David, said:
"Lets keep it simple: only checking for words. No handling for spaces, punctuation or numbers."

But if we're handling capitals and spaces, how about:

let p=s=>(t=>[...t].reverse().join("")==t)(s.toLowerCase().replace(/\s/g,""))

Here,

p("Taco cat") //returns true,
p("Arara") //returns true

Another slight mod and we can ignore all non-letter chars (numbers, punctuation, etc.):

let p=s=>(t=>[...t].reverse().join("")==t)(s.toLowerCase().replace(/[^A-z]/g,""))

p("A man, a plan, a canal: Panama.") // returns true
Collapse
aspenjames profile image
Aspen James

Does this need to work on palindromes containing capitalization, spaces, punctuation, etc?

For example: "A man, a plan, a canal: Panama." - some would consider this a palindrome, some would

Collapse
daveturissini profile image
David Author

Good question. Lets keep it simple: only checking for words. No handling for spaces, punctuation or numbers.

Collapse
maxart2501 profile image
Massimo Artizzu
[...word].reverse().join('')==word

On the plus side, it works on emojis (.split('') doesn't), but not on complex ones (like πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦).

Collapse
daveturissini profile image
David Author
let p = (w) => { let i = 0,c = w[i],l=w.length - 1;while(c){if(c !== w[l-i]){return false}c=w[++c]}return true}
Collapse
n8chz profile image
Lorraine Lee
function p(a) {while (a.length>1&&(v=a.shift()==a.pop())); return v;}