DEV Community

Madalina Pastiu
Madalina Pastiu

Posted on

Break camelCase

Instructions:

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

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

Solution:

function solution(string) {
  return arr = string.split('').map(l => l !== l.toUpperCase() ? l : ' ' + l).join('');

}
Enter fullscreen mode Exit fullscreen mode

Thoughts:

1.I transform the string into an array to be able to iterate easily through each letter/element of the array.
2.I use the function map in order to go through each letter and transformed it based on the condition: l !==l.toUpperCase() .If the original letter is not upper case we return the letter as it is, in case it is upper case , we add a space before the upper letter.

  1. I join the array to a string and return it .

This is a CodeWars Challenge of 6kyu Rank

Top comments (0)