this simple program I am making solution isn't returning the intended output.
if the string includes the phrase camelCasing the expect output should be camel Casing also, if the string includes any other words like camelCasingCarrier the output should be camel Casing Carrier
if the string includes any other phrase, it should just return the string.
function solution(string) {
let newString = null;
let stringToAdd = ' '
if (string.includes('camelCasing')) {
for (let i = 0; i < string.length; i++) {
let letter = string[i];
if (letter == letter.toUpperCase()) {
newString = string.substring(0, letter) + stringToAdd + string.substring(letter);
}
}
return newString;
} else {
return string;
}
}
My current output is camelCasingCarrier with a space at the beginning rather than the expected output. What is my issue and how should I fix this? Thanks again :)
UPDATE
Corrected the prior codes errors here:
function solution(string) {
let newString = '';
let stringToAdd = ' '
if (string.includes('camelCasing')) {
for (let i = 0; i < string.length; i++) {
let letter = string[i];
if (letter == letter.toUpperCase()) {
newString += ' ' + letter;
} else {
newString += letter;
}
}
return newString;
} else {
return string;
}
}