JavaScript Katas: Sort and Star
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-29 21:52:51

---
title: "JavaScript Katas: Sort and Star"
description: "We learn how to use sort, split and 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/57cfdf34902f6ba3d300001e)

Write a function `sortAndStar`, that accepts one parameter: `stringArray`.

Given an array, e.g. `["We", "solve", "Katas"]`,
sort it alphabetically (case-sensitive) and return the first string of the sorted array, with all characters separated by `***` between each other, e.g. `"K***a***t***a***s"`.

---

Input: an array of strings.

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. Sort the array (case-sensitive)
1. Take the first array element
1. Put `***` between each char

---

Example:

- Input: `["We", "solve", "Katas"]`
- Sort array: `["Katas", "We", "solve"]` (case-sensitive)
- Take first array element: `"Katas"`
- Put `***` between each char: `"K***a***t***a***s"`
- Output: `"K***a***t***a***s"` ✅

---

## Implementation ⛑

```js
function sortAndStar(stringArray) {
  const sorted = stringArray.sort(); // the default sort function works
  return sorted[0] // take first element
    .split("") // split string into chars
    .join("***"); // join chars with ***
}
```

### Result

```js
console.log(sortAndStar(["We", "solve", "Katas"]));
// "K***a***t***a***s" ✅
```

---

## Playground ⚽

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

---

## Next Part ➡️

Great work!

We learned how to use `sort`, `split` and `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 📖

- [sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
- [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
- [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?