JavaScript Katas: Is it a palindrome?
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-30 19:04:40

---
title: "JavaScript Katas: Is it a palindrome?"
description: "We learn how to use toLowerCase, split, reverse, join."
published: true
tags: ["JavaScript", "Beginners", "Webdev", "Codenewbie"]
series: JavaScript Katas with miku86
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/0jfyzv0grswtx1mlo17m.jpg
---

## 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](https://www.codewars.com/kata/57a1fd2ce298a731b20006a4)

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

Given a string, e.g. `"Abba"`,
return if this string is a [palindrome](https://en.wikipedia.org/wiki/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
1. Get the backward version of the lowercase string (= reverse it)
1. 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 ⛑

```js
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;
}
```

### Result

```js
console.log(isPalindrome("Abba"));
// true ✅

console.log(isPalindrome("hello"));
// false ✅
```

---

## Playground ⚽

You can play around with the code [here](https://repl.it/@miku86/js-katas-is-it-a-palindrome)

---

## 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](https://forms.gle/RmQLq7CLDiD2Ptmw5).

If you want to read my latest stuff, [get in touch with me!](https://miku86.com/newsletter)

---

## Further Reading 📖

- [toLowerCase](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase)
- [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
- [reverse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)
- [join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)

---

## Questions ❔

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