JavaScript Katas: Divisible By
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-18 13:03:57

---
title: "JavaScript Katas: Divisible By"
description: "We learn how to use the for loop and the filter method"
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/55edaba99da3a9c84000003b)

---

## 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 `divisibleBy`, that accepts two parameters: `numbers` and `divisor`.

Given a numbers array, e.g. `[1, 2, 3, 4, 5, 6]`,
and a divisor, e.g. `2`,
return an array with all numbers divisible by the given divisor, e.g. `[2, 4, 6]`.

---

Input: a numbers array and a number.

Output: a numbers array.

---

## 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. Loop over every number
1. Check if current number is divisible by divisor
1. If yes, save it into results variable
1. Return results

---

Example:

- Input: `([1, 2, 3, 4, 5, 6], 2)`
- Iteration 1: Is `1` divisible by `2`? `No`
- Iteration 2: Is `2` divisible by `2`? `Yes` => Save it into results variable => `[2]`
- Iteration 3: Is `3` divisible by `2`? `No`
- Iteration 4: Is `4` divisible by `2`? `Yes` => Save it into results variable => `[2, 4]`
- Iteration 5: Is `5` divisible by `2`? `No`
- Iteration 6: Is `6` divisible by `2`? `Yes` => Save it into results variable => `[2, 4, 6]`
- Output: `[2, 4, 6]` ✅

---

## Implementation (for loop) ⛑

```js
function divisibleBy(numbers, divisor) {
  // create variable for results
  const result = [];

  // loop over numbers
  for (const n of numbers) {
    // check if current number is divisible by divisor
    if (n % divisor === 0) {
      // if yes, save it into results variable
      result.push(n);
    }
  }

  // return results
  return result;
}
```

## Result

```js
console.log(divisibleBy([1, 2, 3, 4, 5, 6], 2));
// [ 2, 4, 6 ] ✅

console.log(divisibleBy([1, 2, 3, 4, 5, 6], 3));
// [ 3, 6 ] ✅
```

---

## Implementation (functional) ⛑

```js
function divisibleBy(numbers, divisor) {
  // return each number that is divisible by the divisor
  return numbers.filter((n) => n % divisor === 0);
}
```

## Result

```js
console.log(divisibleBy([1, 2, 3, 4, 5, 6], 2));
// [ 2, 4, 6 ] ✅

console.log(divisibleBy([1, 2, 3, 4, 5, 6], 3));
// [ 3, 6 ] ✅
```

---

## Playground ⚽

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

---

## Next Part ➡️

Great work, mate!

We learned how to use the `for of`-loop and the `filter` method.

I hope that 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 📖

- [for of loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)
- [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

---

## Questions ❔

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