DEV Community

Cover image for JavaScript Katas: Count sheep
miku86
miku86

Posted on

JavaScript Katas: Count sheep

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

Result

console.log(countSheep(3));
// 1 sheep...2 sheep...3 sheep...

console.log(countSheep(1));
// 1 sheep...
Enter fullscreen mode Exit fullscreen mode

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("")
  )
}
Enter fullscreen mode Exit fullscreen mode

Result

console.log(countSheep(3));
// 1 sheep...2 sheep...3 sheep...

console.log(countSheep(1));
// 1 sheep...
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
miku86 profile image
miku86

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!

Collapse
 
kosich profile image
Kostia Palchyk • Edited

Nice new kata, Michael!

[...Array(amountOfSheep)] β€” nowadays, I tend to use Array(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 😎:

i=1, 'N sheep...,'.repeat(amountOfSheep).replace(/N/g, () => i++).replace(/,$/,'');
Collapse
 
miku86 profile image
miku86

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.