JavaScript Katas: Split a number array into odd and even numbers
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-09 14:58:26

---
title: "JavaScript Katas: Split a number array into odd and even numbers"
description: "JavaScript Katas: Split a number array into odd and even numbers"
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 🌐

Today, I start a new series about code katas.

I will 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/5721a78c283129e416000999)

---

## 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 `splitOddAndEven`, that accepts one parameter: `numbers`, an array of positive numbers.

The function should return an object with two arrays in it, one for all `odd` numbers and one for all `even` numbers.

---

Input: an array of numbers.

Output: an object with two arrays of numbers, one for the odd ones, one for the even ones.

---

## 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.

- loop over the input array
- if number is odd, save it in a variable `odd`
- if number is even, save it in a variable `even`
- return `odd` and `even` in an object

---

Example:

- Input: `[1, 2, 3]`
- Round 1: `odd = [1]` // first number in array is 1, which is odd, therefore goes into odd array
- Round 2: `even = [2]` // second number in array is 2, which is even, therefore goes into even array
- Round 3: `odd = [1, 3]` // third number in array is 3, which is odd, therefore goes into odd array, too
- Output: `{ odd: [1, 3], even: [2] }` // put odd array and even array in an object

---

## Implementation (for loop) ⛑

```js
function splitOddAndEven(numbers) {
  let odd = [];
  let even = [];

  for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] % 2 === 0) {
      // number is even
      even.push(numbers[i]);
    } else {
      // number is not even (=odd)
      odd.push(numbers[i]);
    }
  }

  // create an object with the odd and even array in it
  const returnObject = {
    odd,
    even,
  };

  return returnObject;
}
```

## Result

```js
console.log(splitOddAndEven([1, 2, 3]));
// { odd: [ 1, 3 ], even: [ 2 ] }

console.log(splitOddAndEven([0, 3, 5]));
// { odd: [ 3, 5 ], even: [ 0 ] }
```

---

## Implementation (for of-loop) ⛑

```js
function splitOddAndEven(numbers) {
  let odd = [];
  let even = [];

  for (const number of numbers) {
    if (number % 2 === 0) {
      // number is even
      even.push(number);
    } else {
      // number is not even (=odd)
      odd.push(number);
    }
  }

  // create an object with the odd and even array in it
  const returnObject = {
    odd,
    even,
  };

  return returnObject;
}
```

## Result

```js
console.log(splitOddAndEven([1, 2, 3]));
// { odd: [ 1, 3 ], even: [ 2 ] }

console.log(splitOddAndEven([0, 3, 5]));
// { odd: [ 3, 5 ], even: [ 0 ] }
```

---

## Implementation (Functional) ⛑

```js
function splitOddAndEven(numbers) {
  // filter out the odd numbers
  const odd = numbers.filter((number) => number % 2 === 1);

  // filter out the even numbers
  const even = numbers.filter((number) => number % 2 === 0);

  // create an object with the odd and even array in it
  const returnObject = {
    odd,
    even,
  };

  return returnObject;
}
```

## Result

```js
console.log(splitOddAndEven([1, 2, 3]));
// { odd: [ 1, 3 ], even: [ 2 ] }

console.log(splitOddAndEven([0, 3, 5]));
// { odd: [ 3, 5 ], even: [ 0 ] }
```

---

## Playground ⚽

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

---

## 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).

I would love to get in touch with you!

---

## Further Reading 📖

- [for-loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)
- [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 ❔

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