JavaScript Katas: Find Odd Digits
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-08-21 16:57:09

---
title: "JavaScript Katas: Find Odd Digits"
description: "We learn how to use String, Number, split, join, filter, slice"
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

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

Source: [Codewars](https://www.codewars.com/kata/59e8afdd0863c7bcb300013a)

Write a function `findOddDigits`, that accepts two parameter: `n` a number and `k` a number.

Given `n`, e.g. `123456789111`, and `k`, e.g. `5`, return the first `k` odd digits in `n`, e.g. `13579`.

In the following cases the function should return `0`:

- there are no odd digits in `n`;
- `k` is bigger than `n`;
- `k` is `0`;
- `k` is bigger than the number of odd digits in `n`.

---

Input: two numbers.

Output: one number.

---

## 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. Find all odd digits
1. Find the first `k` digits
1. Handle all edge cases
1. Return the number

---

Example:

- Input: `123456789111, 5`
- Find all odd digits: `13579111`
- Find the first `k` (= `5`) digits: `13579`
- Return the number: `13579`
- Output: `13579` ✅

---

## Implementation (Explicit) ⛑

```js
function findOddDigits(n, k) {
  // k = 0;
  // k is bigger than a number of digits in n;
  if (k === 0 || k > n) return 0;

  // find all odd digits
  const str = String(n);
  const split = str.split("");
  const odds = split.filter((num) => num % 2);

  // there are no odd digits in a number n;
  // k is bigger than a number of odd digits in n.
  if (!odds.length || k > odds.length) return 0;

  // find the first `k` digits
  const joined = odds.join("");
  const sliced = joined.slice(0, k);

  // return the number
  return Number(sliced);
}
```

### Result

```js
console.log(findOddDigits(123456789111, 5));
// 13579 ✅

console.log(findOddDigits(0, 100));
// 0 ✅
```

---

## Implementation (Implicit) ⛑

```js
function findOddDigits(n, k) {
  // find all odd digits
  const odds = String(n)
    .split("")
    .filter((num) => num % 2);

  // handle all edge cases
  if (k === 0 || k > n || !odds.length || k > odds.length) return 0;

  // find the first `k` digits and return them as a number
  return Number(odds.join("").slice(0, k));
}
```

### Result

```js
console.log(findOddDigits(123456789111, 5));
// 13579 ✅

console.log(findOddDigits(0, 100));
// 0 ✅
```

---

## Playground ⚽

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

---

## Next Part ➡️

Great work!

We learned how to use `String`, `Number`, `split`, `join`, `filter`, `slice`.

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 📖

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

---

## Questions ❔

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