Hi,
Hope you could help me please,
There is the “Search and Replace” challenge
and I wrote this code:
function myReplace(str, before, after) {
var ans="";
var index=str.indexOf(before);
var prevStr=str.substr(0,index);
var nextStr=str.substr(index+1+before.length,str.length);
if(before.charAt(0).charCodeAt()>=65 && before.charAt(0).charCodeAt()<=90){
var c= after.charAt(0).toUpperCase();
after=c+after.substr(1,after.length);
}
ans=prevStr+after+" "+nextStr;
//ans=ans.substr(0,ans.length-1);
return ans;
}
myReplace("He is Sleeping on the couch", "Sleeping", "sitting");
From some reason, if I disable the comment on the line "ans=ans.substr(0,ans.length-1);"
some test are working and other do not, and if I comment that line so the test that worked, not working, and those who not work, are working.
Hope I was cleare enough…
You can just copy and paste and see for yourselves…
Thank you