← Blog

JavaScript Katas: Polish Alphabet

July, 2020

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


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
  2. Loop over every character
  3. Check if the current character has a diacritic
  4. If yes (= has a diacritic), replace it with the character without diacritic
  5. 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) ⛑

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

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

Implementation (functional) ⛑

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

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

Playground ⚽

You can play around with the code here


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.

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?
  • What’s your simplest solution to add uppercase letters?

miku86

Hi! I'm Michael 👋 I'm a Mentor & Educator & Senior Web Developer - I help you to reach your (career) goals.