Here’s a simple, straightforward solution (white space between lines for clarity):
function myReplace(str, before, after) {
\\ IF statement checks to see if the first letter of before (before[0]) is equal to upper case
if (before[0] === before[0].toUpperCase()) {
\\ if before[0] IS upper case, then sets after equal to after with the first letter replaced with upper case
after = after.replace(after[0], after[0].toUpperCase());
}
return str.replace(before, after);
}
myReplace("His name is Tom", "Tom", "john"); // returns "His name is John"
Thanks, @Rafase282, for all of your posts with helpful hints!
if (before[0] === before[0].toUpperCase()) {
after = after[0].toUpperCase() + after.slice(1);
}
str = str.replace(before, after);
return str;
}
myReplace(“A quick brown fox jumped over the lazy dog”, “jumped”, “leaped”);
Based on the things we have learned it seemed the easiest solution. Granted it only works on the premise that only the first letter of the word will be capitalised, but it passes all the tests set.
function myReplace(str, before, after) {
if (str.charCodeAt(str.indexOf(before)) >= 65 && str.charCodeAt(str.indexOf(before)) <= 90) {
return str.replace(before, after.charAt(0).toUpperCase() + after.slice(1));
}
return str.replace(before, after);
}
myReplace("Let us get back to more Coding", "Coding", "algorithms");
Am I the only one who solved this by checking the unicode values? Probably not as eloquent as the solutions above but it works!
Here is my solution with comments to clarify.
`function myReplace(str, before, after) {
if (before[0] === before[0].toUpperCase()){ //checks if the first character of before is capital
after = after.charAt(0).toUpperCase() + after.slice(1); //if the first character of before is capital then the first character of after is made into a capital
}
var newStr = str.replace(before, after); //create a new string where the before word is replaced with the after word
return newStr; //return that new string
}
myReplace(“A quick brown fox Jumped over the lazy dog”, “Jumped”, “leaped”);`
function myReplace(str, before, after) {
if (/[A-Z]/.test(before[0])) {
after = after.replace(after[0],after[0].toUpperCase());
}
return str.replace(before,after);
}
I like this one, more efficient than mine (which is below); one note however, yours could return an unexpected uppercase in ‘after’ if ‘before’ has a number in it. Easily remedied though.
function myReplace(str, before, after) {
var toReplace = str.substr(str.indexOf(before), before.length).split("");
if (toReplace[0].toUpperCase() == toReplace[0]) {
after = after.split("")[0].toUpperCase() + after.substr(1);
}
return str.replace(before, after);
}
//test
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
All of these solutions are all well and good and pass the tests, but what if you need to replace more than one instance of a word in str and maintain case? All of these solutions fail in that case. See https://github.com/freeCodeCamp/freeCodeCamp/issues/15260