This code works, but it’s my first time creating additional functions to make the desired one work, so I would really like some feedback on my code. Would really appreciate it!
function myReplace(str, before, after) {
var result;
// check if before's first letter is uppercase or not, to preserve the case of original word
if (isFirstUpperCase(before)) {
result = str.replace(before, toFirstUpperCase(after));
} else {
result = str.replace(before, after);
}
return result;
}
// function to check whether first character of a string is uppercase
function isFirstUpperCase(string) {
return (string[0] === string[0].toUpperCase());
}
// function to convert first character of a string to uppercase
function toFirstUpperCase(string) {
return string[0].toUpperCase() + string.substr(1);
}