So I created this function but i have an error:
TypeError: Cannot assign to read only property ‘0’ of string ‘sitting’
function myReplace(str, before, after) {
var arr = str.split(" ");
for(var i = 0; i < arr.length + 1; i++)
{
if(arr[i] == before)
{
if(before[0] === before[0].toUpperCase())
{
after[0] = after[0].toUpperCase();
arr[i] = after;
}
}
}
return arr.join(" ");
}
myReplace("He is Sleeping on the couch", "Sleeping", "sitting");```