DEV Community

Cover image for JavaScript Katas: Is it a palindrome?
miku86
miku86

Posted on

JavaScript Katas: Is it a palindrome?

Intro 🌐

Problem solving is an important skill, for your career and your life in general.

That's why I take interesting katas of all levels, customize them and explain how to solve them.


Understanding the Exercise❗

First, we need to understand the exercise!
If you don't understand it, you can't solve it!.

My personal method:

  1. Input: What do I put in?
  2. Output: What do I want to get out?

Today's exercise

Source: Codewars

Write a function isPalindrome, that accepts one parameter: myString.

Given a string, e.g. "Abba",
return if this string is a palindrome (case-insensitive), e.g. true.

A palindrome is a word [...] which reads the same backward as forward, such as racecar.


Input: a string.

Output: a boolean.


Thinking about the Solution πŸ’­

I think I understand the exercise (= what I put into the function and what I want to get out of it).

Now, I need the specific steps to get from input to output.

I try to do this in small baby steps:

  1. Transform the input string to lowercase
  2. Get the backward version of the lowercase string (= reverse it)
  3. Check if the forward string is the same as the backward string

Example:

  • Input: "Abba"
  • Transform the input string to lowercase: "abba"
  • Get the backward version of the lowercase string: "abba"
  • Check if the forward string is the same as the backward string: true
  • Output: true βœ…

Implementation β›‘

function isPalindrome(myString) {
  // transform the input string to lowercase
  const lowercaseInput = myString.toLowerCase();

  // to have a similar wording
  const forward = lowercaseInput;

  // get the backward version of the lowercase string
  const backward = lowercaseInput.split("").reverse().join("");

  // check if the forward string is the same as the backward string
  return forward === backward;
}
Enter fullscreen mode Exit fullscreen mode

Result

console.log(isPalindrome("Abba"));
// true βœ…

console.log(isPalindrome("hello"));
// false βœ…
Enter fullscreen mode Exit fullscreen mode

Playground ⚽

You can play around with the code here


Next Part ➑️

Great work!

We learned how to use toLowerCase, split, reverse, join.

I hope you can use your new learnings to solve problems more easily!

Next time, we'll solve another interesting kata. Stay tuned!


If I should solve a specific kata, shoot me a message here.

If you want to read my latest stuff, get in touch with me!


Further Reading πŸ“–


Questions ❔

  • How often do you do katas?
  • Which implementation do you like more? Why?
  • Any alternative solution?

Top comments (7)

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

Collapse
 
mohsenalyafei profile image
Mohsen Alyafei

Thanks.
It can be done as a one-liner:

function IsPalindrome(str) {
 return str.toLowerCase() === str.split("").reverse().join("").toLowerCase();
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
miku86 profile image
miku86

Hey Mohsen,

nice work!

Or for the lazy folks:

const isPalindrome = (s) => s.toLowerCase() === s.split("").reverse().join("").toLowerCase()
Enter fullscreen mode Exit fullscreen mode