JavaScript Katas: Split a number array into odd and even numbers

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


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) ⛑

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

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) ⛑

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

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

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

Implementation (Functional) ⛑

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

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


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.

I would love to get in touch with you!


Further Reading 📖


Questions ❔

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