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 ππ
Top comments (14)
Short:
Shorter:
In both:
(First works on palindromic arrays of characters as well as strings, second only on strings)
Nice. Really like the approach with βsomeβ in your first example
On the plus side, it works on emojis (
.split('')
doesn't), but not on complex ones (like π¨βπ©βπ§βπ¦).you can shave off 2 chars by doing
.join
``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
Good question. Lets keep it simple: only checking for words. No handling for spaces, punctuation or numbers.
Nice! You can make the function even smaller if you just returned
Even shorter
This wouldn't work for "Arara" would it? It'd need some sort of case normalisation.
Also, what about "Taco cat".
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:
Here,
Another slight mod and we can ignore all non-letter chars (numbers, punctuation, etc.):
This is the shortest IMO:
With normalization: