DEV Community

Cover image for JavaScript Katas: Repeat it
miku86
miku86

Posted on

JavaScript Katas: Repeat it

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

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
  2. 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) ⛑

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

Result

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

console.log(repeatIt(999, 1));
// "Not a string" ✅
Enter fullscreen mode Exit fullscreen mode

Implementation (for loop) ⛑

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

Result

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

console.log(repeatIt(999, 1));
// "Not a string" ✅
Enter fullscreen mode Exit fullscreen mode

Playground ⚽

You can play around with the code here


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.

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 (1)

Collapse
 
rrdlpl profile image
R • Edited
const repeat = (word: string, n: number): string => {
  if (typeof word !== 'string') {
    return 'Not a string';
  }
  return n > 0 ? Array(n).fill(word).join('') : '';
}