JavaScript Katas: Abbreviate a two word name
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-15 11:01:11

---
title: "JavaScript Katas: Abbreviate a two word name"
description: "We learn how to use functional methods"
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/57eadb7ecd143f4c9c0000a3)

---

## 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 `abbreviateName`, that accepts one parameter: `name`.

`name` is a string that includes two words, with one space in between them, e.g. "John Doe".

The output should be the two first characters, both capitalized with a dot separating them, e.g. "J.D".

"John Doe" => "J.D"

"john doe" => "J.D"

---

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. split the name into two words
1. take the first character of the word
1. capitalize this character
1. do this for both words
1. join both characters with a dot
1. return the joined characters

---

Example:

- Input: `"john doe"`
- Split them: `["john", "doe"]`
- Take 1st char: `"j"` // 1st word
- Capitalize it: `"J"` // 1st word
- Take 1st char: `"d"` // 2nd word
- Capitalize it: `"D"` // 2nd word
- Join them with a dot: `"J.D"`
- Output: `"J.D"`

---

## Implementation (functional) ⛑

```js
function abbreviateName(name) {
  return (
    name
      // split the name into two words
      .split(" ")

      // take first char of both parts and capitalize them
      .map((part) => part[0].toUpperCase())

      // join both characters with a dot
      .join(".")
  );
}
```

## Result

```js
console.log(abbreviateName("John Doe"));
// J.D

console.log(abbreviateName("john doe"));
// J.D
```

---

## Playground ⚽

You can play around with the code [here](https://repl.it/@miku86/js-katas-abbreviate-two-word-name)

---

## Next Part ➡️

Great work, mate!

Next time, we'll solve the next 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)

---

## Questions ❔

- Do you like to solve katas?
- Which implementation do you like more? Why?
- Any alternative solution?