← Blog

JavaScript Katas: Count sheep

July, 2020

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

  1. create a variable to save the result
  2. create message based on the current number
  3. add it to the result variable
  4. repeat this until reaching the last number (= amountOfSheep)
  5. 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?

miku86

Hi! I'm Michael 👋 I'm a Mentor & Educator & Senior Web Developer - I help you to reach your (career) goals.