← Blog

August, 2020

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

Write a function filterHomogenous, that accepts one parameter: myArrays.

Given an array of arrays, e.g. [[1, 5, 4], ['a', 3, 5], ['b'], [], ['1', 2, 3]],

return a new array which carries over only those arrays from the original which are not empty and whose items are all of the same type, e.g. [[1, 5, 4], ['b']].

Implicit type casting is not allowed, e.g. [1, '2'] would be considered illegal and should be filtered out.

Input: an array of arrays.

Output: an array of arrays.

## 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. Find out if a specific array is not empty
2. Find out if all elements of a specific array have the same type
3. Filter the input array for all arrays that pass both

Example:

• Input: [[1, 5, 4], ['a', 3, 5], ['b'], [], ['1', 2, 3]]
• Find out if array is not empty & if all elements have the same type: [1, 5, 4] => not empty: true, same type: true => add to result array: true
• Find out if array is not empty & if all elements have the same type: ['a', 3, 5] => not empty: true, same type: false => add to result array: false
• Find out if array is not empty & if all elements have the same type: ['b'] => not empty: true, same type: true => add to result array: true
• Find out if array is not empty & if all elements have the same type: [] => not empty: false, same type: true => add to result array: false
• Find out if array is not empty & if all elements have the same type: ['1', 2, 3] => not empty: true, same type: false => add to result array: false
• Filter the input array for all arrays that pass both: [[1, 5, 4], ['b']]
• Output: [[1, 5, 4], ['b']]

## Implementation ⛑

function filterHomogenous(myArrays) {
const isHomogenous = (array) =>
array.every((el, i, arr) => typeof el === typeof arr[0]);
return myArrays.filter((array) => array.length && isHomogenous(array));
}

### Result

console.log([[1, 5, 4], ["a", 3, 5], ["b"], [], ["1", 2, 3]]);
// [[1, 5, 4], ['b']] ✅

console.log([[123, 234, 432], ["", "abc"], [""], ["", 1], ["", "1"], []]);
// [[123, 234, 432], ['', 'abc'], [''], ['', '1']] ✅

## Playground ⚽

You can play around with the code here

## Next Part ➡️

Great work!

We learned how to use every and filter.

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.

If you want to read my latest stuff, get in touch with me!