JavaScript Katas: Freudian Translator
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-08-03 12:21:49

---
title: "JavaScript Katas: Freudian Translator"
description: "We learn how to use split, map, join, replace and some regex."
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/5713bc89c82eff33c60009f7)

Today as small kata with a little bit of historical background about [Sigmund Freud](https://en.wikipedia.org/wiki/Sigmund_Freud).

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

Given a string, e.g. `"Sigmund Freud"`,
return the string with every word replaced by the word `"sex"`, e.g. `"sex sex"`.
An empty string or no argument should result in an empty string.

---

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. Return an empty string if string is empty or no argument
1. Loop over every word of the string
1. Replace each word with `"sex"`
1. Return a string of all new words

---

Example:

- Input: `"Sigmund Freud"`
- Return an empty string if string is empty or no argument: `false`
- Iteration 1: "Sigmund" => replace it => `"sex"`
- Iteration 2: "Freud" => replace it => `"sex"`
- Return a string of all new words: `"sex sex"`
- Output: `"sex sex"` ✅

---

## Implementation (functional) ⛑

```js
function toFreud(myString) {
  return !myString
    ? "" // return an empty string if string is empty or no argument
    : myString
        .split(" ") // split string into words
        .map((word) => "sex") // replace each word with `"sex"`
        .join(" "); // put replaced words together
}
```

### Result

```js
console.log(toFreud("Sigmund Freud"));
// "sex sex" ✅

console.log(toFreud(""));
// ""  ✅
```

---

## Implementation (regex) ⛑

```js
function toFreudRegex(myString) {
  return !myString
    ? ""
    : myString.replace(/\S+/g, "sex"); // find all places that have "one or more non-white space", replace them with "sex"
}
```

The regex `\S` character class "matches a single character other than white space". You can read about it [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes).

The regex `+` quantifier "matches the preceding item 1 or more times". You can read about it [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Quantifiers).

### Result

```js
console.log(toFreud("Sigmund Freud"));
// "sex sex" ✅

console.log(toFreud(""));
// ""  ✅
```

---

## Playground ⚽

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

---

## Next Part ➡️

Great work!

We learned how to use `split`, `map`, `join`, `replace` and some regex.

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 📖

- [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
- [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
- [join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)
- [replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)
- [regex character classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes)
- [regex quantifiers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Quantifiers)

---

## Questions ❔

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