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:
- Input: What do I put in?
- 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.
- Loop over every number
- Check if current number is divisible by divisor
- If yes, save it into results variable
- Return results
Example:
- Input:
([1, 2, 3, 4, 5, 6], 2)
- Iteration 1: Is
1
divisible by2
?No
- Iteration 2: Is
2
divisible by2
?Yes
=> Save it into results variable =>[2]
- Iteration 3: Is
3
divisible by2
?No
- Iteration 4: Is
4
divisible by2
?Yes
=> Save it into results variable =>[2, 4]
- Iteration 5: Is
5
divisible by2
?No
- Iteration 6: Is
6
divisible by2
?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;
}
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 ] ✅
Implementation (functional) ⛑
function divisibleBy(numbers, divisor) {
// return each number that is divisible by the divisor
return numbers.filter((n) => n % divisor === 0);
}
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 ] ✅
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)