DEV Community

Madalina Pastiu
Madalina Pastiu

Posted on

Double Char

Instructions:

Given a string, you have to return a string in which each character (case-sensitive) is repeated once.

Examples (Input -> Output):

  • "String" -> "SSttrriinngg"
  • "Hello World" -> "HHeelllloo WWoorrlldd"
  • "1234!_ " -> "11223344!!__ "

Thoughts:

  1. I transform the string to an array and split it into individual letters.
  2. I use the map() method to replace each letter to a double letter and join the array to a string.

Solution:

function doubleChar(str) {
 const array = str
    .split("")
    .map((arr) => arr.replace(arr, arr + arr))
    .join("");
  return array;
}
Enter fullscreen mode Exit fullscreen mode

This is a CodeWars Challenge of 8kyu Rank

Top comments (0)