Hey zealsham
I solved it like this. As my dad says there’s more then one way to skin a cat
I prefer more spaces to make code easier to read but thats just a personal pref.
I was originally using arr.reduce to return the arr to a string but join() was way simpler so i copied that from you.
function myReplace(str, before, after) {
after = after.split('');
before = before.split('');
if (before[0] == before[0].toUpperCase()) after[0] = after[0].toUpperCase();
after = after.join('');
before = before.join('');
var newStr = str.replace(before, after);
return newStr;
}
myReplace("He is Sleeping on the couch", "Sleeping", "sitting")
There is an unused variable name, other than that everything is fine.
Also, you are supposed to use this string method when you replace the old string with new one.
You have split the string into words and replaced the required word, like you would do in earlier challenges. But for the sake of confirming to the present challenge, familiarize (if needed) with String.prototype.replace() and modify your code.
Suggestion : else {sam[i]=after};
Saving space like thing might mean more head scratching in the future, always format you code neatly!
Unless the before string is in title case you don’t have to split before and after.
function myReplace(str, before, after) {
if (before[0] == before[0].toUpperCase()){
// only split when you need to preserve case of old substring
after = after.split('');
before = before.split('');
after[0] = after[0].toUpperCase();
after = after.join('');
before = before.join('');
}
var newStr = str.replace(before, after);
return newStr;
}
wow! mine is much longer - but hey it still works!
function myReplace(str, before, after) {
var newafter;
var newstr;
var newWord = [];
if (before.toLowerCase() === before) {
newstr = str.replace(before,after);
} else {
for (i = 0; i<after.length; i++) {
if (i > before.length-1) {
var current3 = after[i];
newWord.push(current3);
} else if(before[i].toLowerCase() === before[i]){
var current = after[i];
newWord.push(current);
} else {
var current2 = after[i].toUpperCase();
newWord.push(current2);
}
}
var joined = newWord.join('');
newstr = str.replace(before, joined);
}
return newstr;
}
function myReplace(str, before, after) {
var chr =before.charAt(0);
if (chr==chr.toUpperCase()) {
after=after.charAt(0).toUpperCase() + after.substring(1);
}
return str.replace(before,after);
}
myReplace(“A
quick brown fox jumped over the lazy dog”, “jumped”,
“leaped”);
function myReplace(str, before, after) {
let final ="";
if(before[0].charCodeAt() < 90)
after = after.replace(after[0],after[0].toUpperCase());
final = str.replace(before,after);
return final;
}
myReplace("He is Sleeping on the couch", "Sleeping", "sitting");
Always interesting to see everyone’s solutions. There’s more than one way to skin a cat!
Edit: Noticing a small inconsistency of using before[0] and before.charAt(0) when I could have just one of those methods in this case. I was experimenting with them, I’ll leave it as a remnant of that.
function myReplace(str, before, after) {
var re = new RegExp(before);
var upAfter = after.charAt(0).toUpperCase() + after.slice(1);
if (before[0] === before[0].toUpperCase()) {
str = str.replace(re, upAfter);
} else {
str = str.replace(re, after);
}
return str;
}
Thanks to the ternary operator, I was able to compress my solution down to a single statement. (This passes the test, but only replaces the first occurrence of a word and assumes after is passed into the function in lowercase.)
function myReplace(str, before, after) {
var regex = new RegExp(/[A-Z]/,'mg');//regex to know if first letter is capital
//if it is then capitalize first letter of after
if(before.match(regex)) after = after.slice(0,1).toUpperCase()+after.slice(1);
//simply use replace
//return str.replace(before,after);
//incase if you don't want to use replace
var index;//index of before string in str
str = str.split(' '); // array
//iterate through array to find the index of before
str.map(function(val,ind){
if(val == before) index = ind; //if value is eql to before, set index varialble to index of before
});
//replace it
str[index] = after;
return str.join(' ');
}`