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 countSheep
, that accepts one parameter: amountOfSheep
.
Given a non-negative number, e.g. 3
, return a string with "1 sheep...2 sheep...3 sheep..."
.
Input: a number (of sheep).
Output: a string.
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.
- create a variable to save the result
- create message based on the current number
- add it to the result variable
- repeat this until reaching the last number (= amountOfSheep)
- return the result
Example:
- Input:
3
- Iteration 1:
["1 sheep..."]
// create message based on the current number, add it to the result - Iteration 2:
["1 sheep...", "2 sheep..."]
// create message based on the current number - Iteration 3:
["1 sheep...", "2 sheep...", "3 sheep..."]
// create message based on the current number - Output:
"1 sheep...2 sheep...3 sheep..."
// create the output string
Implementation (for loop) ⛑
function countSheep(amountOfSheep) {
// create a variable to save the result
let result = "";
for (let i = 1; i <= amountOfSheep; i++) {
// create message based on the current number, add it to the result
result += `${i} sheep...`;
}
return result;
}
Result
console.log(countSheep(3));
// 1 sheep...2 sheep...3 sheep...
console.log(countSheep(1));
// 1 sheep...
Implementation (functional) ⛑
function countSheep(amountOfSheep) {
return (
[...Array(amountOfSheep)]
// create message based on the current number
.map((_, i) => `${i + 1} sheep...`)
// "add" it to the result
.join("")
)
}
Result
console.log(countSheep(3));
// 1 sheep...2 sheep...3 sheep...
console.log(countSheep(1));
// 1 sheep...
Playground ⚽
You can play around with the code here
Next Part ➡️
Great work, mate!
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 (3)
Hey Rob,
I mostly try to start with a procedural solution, like the
for loop
one, so that beginners see something they already know.Then I try to add a functional one to show a different approach.
Glad you like it, thanks!
Nice new kata, Michael!
[...Array(amountOfSheep)]
— nowadays, I tend to useArray(amountOfSheep).fill(null)
for such cases:Theoretically it should be faster/smaller (while the
...
looks definitely cooler!)But I haven't checked that 😊
Also, a hacky approach 😎:
Hey Kostia,
nice solution!
First I used the
fill
one, but I thought it would be interesting for beginners to read about the widely-used spread operator and see it in action.