break-camelCase

title

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

example

1
solution('camelCasing') // => should return 'camel Casing'


My solution

1
2
3
4
5
6
7
8
9
10
11
12
13
function solution(string) {
var newString = string.toLowerCase();
var result = [];
var j = 0;
for(var i = 0;i<string.length;i++){
if(string.charAt(i) != newString.charAt(i)){
result.push(string.slice(j,i));
j = i;
}
}
result.push(string.slice(j));
return result.join(' ');
}

good solution

1
2
3
4
function solution(string) {
return(string.replace(/([A-Z])/g, ' $1'));

}