JavaScript Katas: Polish Alphabet
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-22 16:35:01

---
title: "JavaScript Katas: Polish Alphabet"
description: "We learn how to use the for of loop, Object.keys(), includes and map"
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 🌐

I take interesting katas of all levels and explain how to solve them.

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

You'd better learn to solve problems!

---

## Source

I take the ideas for the katas from different sources and re-write them.

Today's source: [Codewars](https://www.codewars.com/kata/57ab2d6072292dbf7c000039)

---

## Understanding the Exercise ❗

First, we need to understand the exercise!

This is a crucial part of (software) engineering.

Go over the exercise explanation again until you understand it 100%.

Do NOT try to save time here.

My method to do this:

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

---

### Today's exercise

Write a function `correctPolishLetters`, that accepts one parameter: `inputString`.

Given a string, e.g. `Wół`, return a string without the diacritics, e.g. `Wol`.

To keep it simple, we'll just care about the lower case characters.

---

Input: a string.

Output: a string.

---

## 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. Create a mapping of the characters with diacritics and without them
1. Loop over every character
1. Check if the current character has a diacritic
1. If yes (= has a diacritic), replace it with the character without diacritic
1. Return results

---

Example:

- Input: `Wół`
- Iteration 1: `W` has diacritic? => `No` => not replace it => `W`
- Iteration 2: `ó` has diacritic? => `Yes` => replace it => `o`
- Iteration 3: `ł` has diacritic? => `Yes` => replace it => `l`
- Output: `Wol` ✅

---

## Implementation (for loop) ⛑

```js
function correctPolishLetters(inputString) {
  // mapping for characters
  const mapping = {
    ą: "a",
    ć: "c",
    ę: "e",
    ł: "l",
    ń: "n",
    ó: "o",
    ś: "s",
    ź: "z",
    ż: "z",
  };

  // variable to save result
  let withoutDiacritics = "";

  // loop over every number
  for (const char of inputString) {
    // check if mapping has a key with the current character
    if (Object.keys(mapping).includes(char)) {
      withoutDiacritics += mapping[char];
      // if yes, return its replacement
    } else {
      // if not, return it unchanged
      withoutDiacritics += char;
    }
  }

  // return result
  return withoutDiacritics;
}
```

## Result

```js
console.log(correctPolishLetters("Wół"));
// "Wol" ✅
```

---

## Implementation (functional) ⛑

```js
function correctPolishLetters(inputString) {
  // mapping for characters
  const mapping = {
    ą: "a",
    ć: "c",
    ę: "e",
    ł: "l",
    ń: "n",
    ó: "o",
    ś: "s",
    ź: "z",
    ż: "z",
  };

  return (
    inputString
      // split the string into an array
      .split("")
      .map(
        (char) =>
          // check if mapping has a key with the current character
          Object.keys(mapping).includes(char)
            ? mapping[char] // if yes, return its replacement
            : char // if not, return it unchanged
      )
      // join the array to a string
      .join("")
  );
}
```

## Result

```js
console.log(correctPolishLetters("Wół"));
// "Wol" ✅
```

---

## Playground ⚽

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

---

## Next Part ➡️

Great work, mate!

We learned how to use a `for of` loop, `Object.keys()`, `includes` and `map`.

I hope that 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 📖

- [for of loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)
- [Object.keys](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)
- [includes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)
- [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

---

## Questions ❔

- How often do you do katas?
- Which implementation do you like more? Why?
- Any alternative solution?
- What's your simplest solution to add uppercase letters?