DEV Community

Discussion on: JavaScript Katas: Is it a palindrome?

Collapse
 
kosich profile image
Kostia Palchyk

Similar to your functional approach:

const isPalindrome = s => s
  .toLowerCase()
  .split('')
  .every((x, i, a) => x == a[a.length - i - 1])
Collapse
 
miku86 profile image
miku86

Hey Kostia,

nice use of every!
Maybe it will be very slow when the input string is very long,

Collapse
 
kosich profile image
Kostia Palchyk • Edited

Good point, Michael, it might!

While, reverse-ing and join-ing a big array of letters might be slow too 😉

I think Katas are rarely good for training optimization skills, but often for problem solving skills.

Usually optimization is done after the problem is solved (or bigger feature implemented) and we've measured and found issues with performance. Palindromes are often short, but your point holds: this method's performance should be measured in practice.

And in real life projects I tend to write more readable and flexible code 🙂

P.S. I'm particularly proud with this kata solution:

Okay, I'm enjoying posting weird solutions :)

const getStatusOfWellOfIdeas = a => [
    "Fail!", "Publish!", "Publish!", "I smell a series!"
  ][Math.min(3, a.join('').length - a.length * 3)]

Surely, I wont write such code IRL

Collapse
 
rainson12 profile image
Rainson12 • Edited

I think you also only need to compare the string length /2 +1 in order to check whether it's a palindrome because the first half actually needs to be the same like the last half only in reverse order right? So a for loop and then accessing the index of the string for the length /2+1 might be the fastest solution?

Collapse
 
kosich profile image
Kostia Palchyk

yep, it would probably be the fastest solution