Initial questions on Search and Replace

Tell us what’s happening:
I’m starting the process of figuring this problem out and I’d just like a little bit of critique about my approach. This will determine how I think about how to proceed. So just hints about where to go or any gross logical error I’ve made in my comments would really be appreciated.

Thank you!

Your code so far


function myReplace(str, before, after) {
//split string in order to provide more options for array methods
  var newStr = str.split(" ").indexOf(before);

//use replace() method,referencing index of element to be replaced and parameter representing element that replaces it
 var myStr = newStr.replace(str[4], after)

//use join() to return to string type
var finStr = myStr.join(" ")

  return finStr;
}

myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/search-and-replace

Start by logging out the variables.

var newStr = str.split(" ").indexOf(before);
console.log(newStr) // Can you run replace() on this?
// Fixing that
var myStr = ...your code
console.log(newStr) // Can you run join() on myStr?

There’s really no reason to split the entire string.

Just replace before with after. And in the situation of keeping capitalization, just check the capitalization of the first character of the before variable.

I think I have the logic right, but I must be doing something wrong with toUpperCase(). I checked online and it seems I have it right, so I’m a bit stumped.


function myReplace(str, before, after) {
  //ensure after applies same case as before uses
if(after.charAt[0] !== before[0]){
   after[0].toUpperCase();
}
//replace before with after
var myStr = str.replace(before, after);
console.log(myStr)
return myStr;
}


myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

I solved it and I want to go through my logic and have someone critique it.

[spoiler]

  1. You must ensure that before is capitalized by using triple equals to ensure that before(0) is equal to before(0) capitalized (if not then you simply run replace and return.
  2. If this is true, then before can be replaced but after must likewise be capitalized. It must be directly referenced with charAt(0) to ensure that only the first character is capitalized, you must then add the rest of the string and you do this through a slice and adding it to the capitalized character.
    My actual code is below. My query is if I used the most streamlined approach. I think I did, but I wonder if there is a way to do it in two lines…maybe one??
**function** myReplace(str, before, after) {
*//check to see if before is capitalized*
**if** (before.charAt(0) === before.charAt(0).toUpperCase()){
*//if so then replace before with capitalized after on first character*
*//use slice() to add the remaining characters to newly capitalized first character*
str = str.replace(before, after.charAt(0).toUpperCase() + after.slice(1));
*//if before is not capitalized then just replace()*
} **else** {
 str = str.replace(before, after);
}
**return** str;
}[/spoiler]