JavaScript Katas: Repeat it
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-07-27 13:47:21

---
title: "JavaScript Katas: Repeat it"
description: "We learn how to use typeof, repeat and for"
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 🌐

Problem solving is an important skill, for your career and your life in general.

That's why I take interesting katas of all levels, customize them and explain how to solve them.

---

## Understanding the Exercise ❗

First, we need to understand the exercise!
If you don't understand it, you can't solve it!.

My personal method:

1. Input: What do I put in?
2. Output: What do I want to get out?

---

### Today's exercise

Source: [Codewars](https://www.codewars.com/kata/557af9418895e44de7000053)

Write a function `repeatIt`, that accepts two parameters: `inputString` and `repetitions`.

Given a string, e.g. `"Hi"`,
and a number of repetitions, e.g. `2`,
return a string that repeats the input string n number of times, e.g. `"HiHi"`:

If the input is not a string, return "Not a string".

---

Input: a string and a number.

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. Check if it is a string
1. If yes, then repeat it x times

---

Example:

- Input: `"Hi", 2`
- Check if it is a string: `true`
- Iteration 1: add it to results => `"Hi"`
- Iteration 2: add it to results => `"HiHi"`
- Output: `"HiHi"` ✅

---

## Implementation (native method) ⛑

```js
function repeatIt(inputString, repetitions) {
  // check if it is a string
  if (typeof inputString !== "string") {
    return "Not a string";
  }

  // repeat it x times
  return inputString.repeat(repetitions);
}
```

### Result

```js
console.log(repeatIt("Hi", 2));
// "HiHi" ✅

console.log(repeatIt(999, 1));
// "Not a string" ✅
```

---

## Implementation (for loop) ⛑

```js
function repeatIt(inputString, repetitions) {
  // check if it is a string
  if(typeof inputString !== "string"){
    return "Not a string";
  }

  // variable for result
  let result = "";

  // repeat it x times
  for(let i = 0; i < repetitions; i++){
    // add it to result
    result += inputString
  }

  return result;
}
```

### Result

```js
console.log(repeatIt("Hi", 2));
// "HiHi" ✅

console.log(repeatIt(999, 1));
// "Not a string" ✅
```

---

## Playground ⚽

You can play around with the code [here](https://repl.it/@miku86/js-katas-repeat-it)

---

## Next Part ➡️

Great work!

We learned how to use `typeof`, `repeat` and `for`.

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

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

---

## Further Reading 📖

- [typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
- [repeat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat)
- [for loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)

---

## Questions ❔

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