# freeCodeCamp Algorithm Challenge Guide: Search and Replace

freeCodeCamp Algorithm Challenge Guide: Search and Replace
0

#4

## Here’s a simple, straightforward solution: (And thank you, @Rafase282, for all of your hints and help.)

``````function myReplace(str, before, after) {
if (before[0] === before[0].toUpperCase()) {
after = after.replace(after[0], after[0].toUpperCase());
}
return str.replace(before, after);
}
``````

For Array Lovers here is something short and precise =>

``````
function myReplace(str, before, after) {
return str
.split(" ")
.map((x) => (x == before)?
(x[0] == x[0].toUpperCase()) ?
(after[0].toUpperCase() + after.slice(1)) : after.toLowerCase()
: x)
.join(" ");

}
``````

This also takes care if after starts with an upper case character and before is a lowercase string

#5

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"
``````

#6

I solved it like this:-

function myReplace(str, before, after) {

``````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.

#7

Here’s my solution:

``````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!

#8

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”);`

#9

My quick solution:

function myReplace(str, before, after) {
return str.split(before).join(before[0] == before[0].toUpperCase() ? after.charAt(0).toUpperCase() + after.slice(1): after);
}

myReplace(“He is Sleeping on the couch”, “Sleeping”, “sitting”);

Search and Replace - Better Advanced Solution
#10

I used array functions, to come up with this solution that is case-sensitive beyond the first character.

``````function myReplace (str, before, after) {

var boolArr =  before.split("").map(function (bLet) {
return bLet===bLet.toUpperCase() ? true : false;
});

after = after.split("").map(function (aLet) {
return boolArr[after.indexOf(aLet)] ? aLet.toUpperCase() : aLet.toLowerCase();
}).join("");

return str.replace(before,after);
}
``````
1. List item

#11

Great solution. Very efficient!

#12

My solution -

``````function myReplace(str, before, after) {

if (/[A-Z]/.test(before[0])) {
after = after.replace(after[0],after[0].toUpperCase());
}
return str.replace(before,after);
}``````

#14

function myReplace(str, before, after) {

after = after.split(’’);
if(before.charCodeAt(0)>=65 && before.charCodeAt(0)<=90){
var upCase = after[0].toUpperCase();
after.splice(0,1,upCase);

}

return str.replace(before,after.join(’’));

}

#15

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.

Here’s mine:

``````
function myReplace(str, before, after) {
var after1 = "";
for(var i = 0; i < Math.min(before.length, after.length); i++) {
if(isNaN(before.charAt(i)) && before.charAt(i) === before.charAt(i).toUpperCase()) {
after1 += after.charAt(i).toUpperCase();
}
else if(isNaN(before.charAt(i)) && before.charAt(i) === before.charAt(i).toLowerCase()) {
after1 += after.charAt(i).toLowerCase();
}
else {
after1 += after.charAt(i);
}
}
if(after1.length < after.length) {
after1 += after.slice(after1.length - after.length);
}
return str.replace(before, after1);
}

``````

#16

My solution:

``````function myReplace(str, before, after) {
return str.replace(before, () => before[0] == before[0].toUpperCase() ?
after.replace(after[0], after[0].toUpperCase()) : after);
}``````

#17
``````function myReplace(str, before, after) {
//if (before.charCodeAt(0) < 90) {
if (/[A-Z]/.test(before[0])) {
after = after.split('');
after.splice(0, 1, after[0].toUpperCase());
after = after.join('');
}
return str.replace(new RegExp(before, 'g'), after);
}

myReplace("He is Sleeping on the couch", "Sleeping", "sitting");``````

#18

My solution:

``````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");``````

#19

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

#20

This is the best I could come up with that matches the specific case of before and replaces all instances:

``````function myReplace(str, before, after) {

if (/^[A-Z]/.test(before)){
after = after.charAt(0).toUpperCase() + after.slice(1);
}

return str.replace(new RegExp(before, 'g'),after);

}``````

#21

My solutions:

function myReplace(str, before, after) {

``````return str.replace(before, function () {
return before.charCodeAt(0)<=90 ? after.charAt(0).toUpperCase()+after.slice(1) : after.charAt(0).toLowerCase()+after.slice(1);
});
``````

}

and other

function myReplace(str, before, after) {

``````return str.replace(before, function () {
var arrAfter=after.split('');
before.charCodeAt(0)<=90 ? arrAfter.splice(0, 1, after[0].toUpperCase()) : arrAfter.splice(0, 1, after[0].toLowerCase());
return arrAfter.join('');
});
``````

}

#22

this should be an interesting solution

function myReplace(str, before, after) {
return before[0] === before[0].toUpperCase() ?
str.replace(before, after = after.replace(after[0], after[0].toUpperCase())) : str.replace(before,after);
}

myReplace(“A quick brown fox jumped over the lazy dog”, “jumped”, “leaped”);

#23

Here is my not very elegant solution:

``````function myReplace(str, before, after) {

var stringArr = str.split(' ');
var newStr = '';
var regex = /([a-z])([a-zA-Z]*)/g;
var replacer = function (match, p1, p2, p3, offset, string) {
return p1.toUpperCase() + p2;
};

stringArr.forEach(function(element, index){
if (element.indexOf(before) === 0) {
if(/[A-Z]/.test(stringArr[index][0])) {
stringArr[index] = after.replace(regex, replacer);
} else {
stringArr[index] = after;
}
}
newStr = stringArr.join(' ');
});
return newStr;
}``````

#24

if you do not want to use replace function:

``````function myReplace(str, before, after) {
var i = 0;
after = after.toLowerCase();
before = before.split("");
after = after.split("");
for(i = 0; i< before.length; i++){
if(before[i].charCodeAt(0) >= 65 && before[i].charCodeAt(0 ) <= 90) {
after[i] = after[i].toUpperCase();
}
}
after = after.join("");
before = before.join("");
str = str.split(" ");
for(i = 0; i< str.length; i++){
if(str[i] == before) {
str[i] = after;
}
}
str = str.join(" ");
return str;
}

``````