Break camelCase

Lee·2022년 7월 12일

Algorithm

목록 보기
45/92
post-thumbnail

❓ Break camelCase

Q. Complete the solution so that the function will break up camel casing, using a space between words.

Example
"camelCasing" => "camel Casing"
"identifier" => "identifier"
"" => ""

✔ Solution

//#my solution
function solution(string) {
  let arr = string.split("");
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === arr[i].toUpperCase()) {
      arr.splice(i, 1, " " + arr[i]);
    }
  }
  return arr.join("");
}

//#other solution
function solution(string) {
  return string.replace(/([a-z])([A-Z])/g, "$1 $2");
}
profile
Lee

0개의 댓글