JavaScript Katas: Count sheep
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-16 10:40:10

---
title: "JavaScript Katas: Count sheep"
description: "We learn how to use for, spread, map, join"
published: true
tags: ["JavaScript", "Beginners", "Webdev", "Codenewbie"]
series: JavaScript Katas with miku86
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/0jfyzv0grswtx1mlo17m.jpg
---

## 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](https://www.codewars.com/kata/5b077ebdaf15be5c7f000077)

---

## 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
1. create message based on the current number
1. add it to the result variable
1. repeat this until reaching the last number (= amountOfSheep)
1. 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) ⛑

```js
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

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

console.log(countSheep(1));
// 1 sheep...
```

---

## Implementation (functional) ⛑

```js
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

```js
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](https://repl.it/@miku86/js-katas-count-sheep)

---

## 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](https://forms.gle/RmQLq7CLDiD2Ptmw5).

If you want to read my latest stuff, [get in touch with me!](https://miku86.com/newsletter)

---

## Further Reading 📖

- [for loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)
- [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Array)
- [[...]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)
- [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
- [join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)

---

## Questions ❔

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