DEV Community

Cover image for JavaScript Katas: Divisible By
miku86
miku86

Posted on

JavaScript Katas: Divisible By

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


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
  2. Check if current number is divisible by divisor
  3. If yes, save it into results variable
  4. 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) β›‘

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;
}
Enter fullscreen mode Exit fullscreen mode

Result

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 ] βœ…
Enter fullscreen mode Exit fullscreen mode

Implementation (functional) β›‘

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

Result

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 ] βœ…
Enter fullscreen mode Exit fullscreen mode

Playground ⚽

You can play around with the code here


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.

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


Further Reading πŸ“–


Questions ❔

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

Top comments (0)