JavaScript Katas: Letterbox Paint Squad
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-08-27 11:24:37

---
title: "JavaScript Katas: Letterbox Paint Squad"
description: "We learn how to use ..., Array, keys, entries, slice, flat, map, reduce."
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.

---

### Today's exercise

Today, another `7 kyu` kata,
meaning we slightly increase the difficulty.

Source: [Codewars](https://www.codewars.com/kata/597d75744f4190857a00008d)

> You and a group of friends are earning some extra money by re-painting the numbers on people's letterboxes for a small fee.

> Since there are 10 of you in the group, each person just paints one digit, e.g. somebody will paint only the 1's, somebody else will paint only the 2's and so on...

> At the end of the day you realise not everybody did the same amount of work.

> To avoid any fights you need to distribute the money fairly. That's where this Kata comes in.

Write a function `paintLetterboxes`, that accepts two parameter: `start` and `end`.

Given a `start` number, e.g. `125`,
and an `end` number, e.g. `132`,
return the frequency of all 10 digits painted,
e.g. `[1, 9, 6, 3, 0, 1, 1, 1, 1, 1]`.

```
125: 1 x 1, 1 x 2, 1 x 5
126: 1 x 1, 1 x 2, 1 x 6
...
132: 1 x 1, 1 x 3, 1 x 2
```

---

Input: two numbers.

Output: an array of numbers.

---

## Thinking about the Solution 💭

First, we need to understand the exercise! If we don't understand it, we can't solve it!.

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 array with the number of every number between start and end
1. Map each number into its split digits
1. Reduce every number into one big array
1. Create object with all numbers from 0 to 9
1. Count every digit
1. Return as array

---

Example:

- Input: `125, 132`
- Create array with the number of every number between start and end: `[125, 126, 127, 128, 129, 130, 131, 132]`
- Map each number into its split digits: `[ [ '1', '2', '5' ], [ '1', '2', '6' ], ..., [ '1', '3', '2' ] ]`
- Reduce every number into one big array: `[ '1', '2', '5', '1', '2', '6', ..., '1', '3', '2' ]`
- Create object with all numbers from 0 to 9: `{ '0': 0, '1': 0, ... , '9': 0 }`
- Count every digit: `{ '0': 1, '1': 9, ... , '9': 1 }`
- Return as array: `[ 1, 9, 6, 3, 0, 1, 1, 1, 1, 1 ]`
- Output: `[ 1, 9, 6, 3, 0, 1, 1, 1, 1, 1 ]` ✅

🥵

---

## Implementation (Explicit) ⛑

```js
function paintLetterboxes(start, end) {
  // create array with the number of every number between start and end
  // const array = [...Array(end - start + 1).keys()].map(i => i + start);
  const allNumbers = [...Array(end + 1).keys()].slice(start);

  // map each number into its split digits
  const splitIntoDigits = allNumbers.map((num) => String(num).split(""));

  // reduce every number into one big array
  // const allDigits = splitIntoDigits.reduce((acc, cur) => acc.concat(cur), []); // node < 11
  const allDigits = splitIntoDigits.flat();

  // create object with all numbers from 0 to 9
  const startObject = [...Array(10).keys()].reduce(
    (acc, cur) => ({ ...acc, [cur]: 0 }),
    {}
  );

  // count every digit
  const counted = allDigits.reduce(
    (acc, cur) => ({ ...acc, [cur]: acc[cur] + 1 }),
    startObject
  );

  // return as array
  const result = Object.entries(counted).reduce(
    (acc, cur) => [...acc, cur[1]],
    []
  );

  return result;
}
```

### Result

```js
console.log(paintLetterboxes(125, 132));
// [ 1, 9, 6, 3, 0, 1, 1, 1, 1, 1 ] ✅

console.log(paintLetterboxes(2, 4));
// [ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 ] ✅
```

---

## Implementation (Implicit) ⛑

```js
function paintLetterboxes(start, end) {
  const startObject = [...Array(10).keys()].reduce(
    (acc, cur) => ({ ...acc, [cur]: 0 }),
    {}
  );
  const counted = [...Array(end + 1).keys()]
    .slice(start)
    .map((num) => String(num).split(""))
    .flat()
    .reduce((acc, cur) => ({ ...acc, [cur]: acc[cur] + 1 }), startObject);
  return Object.entries(counted).reduce((acc, cur) => [...acc, cur[1]], []);
}
```

### Result

```js
console.log(paintLetterboxes(125, 132));
// [ 1, 9, 6, 3, 0, 1, 1, 1, 1, 1 ] ✅

console.log(paintLetterboxes(2, 4));
// [ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 ] ✅
```

---

## Playground ⚽

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

---

## Next Part ➡️

Great work!

Probably, this solution is way too complicated, but it was fun!

We learned how to use `...`, `Array`, `keys`, `entries`, `slice`, `flat`, `map`, `reduce`.

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 📖

- [...](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)
- [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
- [keys](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys)
- [entries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)
- [slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)
- [flat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat)
- [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
- [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)

---

## Questions ❔

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