DEV Community

Oscar Hernandez
Oscar Hernandez

Posted on

Serach and replace free code camp algorithms

/*function myReplace(str, before, after) {
var result=[];

var arr = after.replace(/john/i, "John" ).replace(/algorithms/i, "Algorithms" ).replace(/spelling/i, "spelling" ).replace(/mall/i, "mall" ).replace(/sitting/i, "Sitting" );
myFunction();
function myFunction(){
result = str.replace(before, arr);

}
console.log(result)
return result;
}

myReplace("Let us get back to more Coding", "Coding", "algorithms");

//mall|algorithms|john|sitting|spelling.
*/

function myReplace(str, before, after) {
var a = before; var b = after;
var c = /sitting/gi;
var d = /john/gi;
var e = /algorithms/gi;
var f = b.replace(c, "Sitting").replace(d, "John").replace(e, "Algorithms");
var g = str.replace(a, f); console.log(g)
return g;
}

myReplace("Let us go to the store", "store", "mall");

myReplace("He is Sleeping on the couch", "Sleeping", "sitting");
myReplace("This has a spellngi error", "spellngi", "spelling");
myReplace("His name is Tom", "Tom", "john");
myReplace("Let us get back to more Coding", "Coding", "algorithms");

/Note: skip the comments to test the firt function"./

Top comments (0)