Double Char

Lee·2022년 9월 1일

Algorithm

목록 보기
87/92
post-thumbnail

❓ Double Char

Q. 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!!__ "
    Good Luck!

✔ Solution

//#my solution
function doubleChar(str) {
  // Your code here
  let result = "";
  for (let i = 0; i < str.length; i++) {
    result += str[i].repeat(2);
  }
  return result;
}

//#other solution
const doubleChar = (str) => str.split("").map(c => c + c).join("");
profile
Lee

0개의 댓글