function myReplace(str, before, after) { if (/[A-Z]/.test(before[0])) { after = after[0].toUpperCase() + after.substring(1, after.length); } var strToArray = str.split(" "); var elementToDelete = strToArray.indexOf(before); //Outputs index 4 return strToArray.join(" "); } myReplace("He is Sleeping on the couch", "Sleeping", "sitting"); myReplace("His name is Tom", "Tom", "john") should return "His name is John". myReplace("This has a spellngi error", "spellngi", "spelling") myReplace("Let us go to the store", "store", "mall")