Need Help for Search and Replace

Need Help for Search and Replace
0

#1

Tell us what’s happening:
My code does not pass because I have to replace the first letter of the word in capital letters, if the word to be replaced had the first letter in capital letters.
Use the following logic, if the first letter of the word to be replaced is equal to the first letter of the word to be replaced in uppercase then I have to modify the first letter of the word to uppercase once it has been replaced.
I know the function is wrong but I don’t know how to make it work.

I just need suggestion, I want to do it by my own. I’ve been stuck for a week.

Thanks in advanced.

Your code so far


function myReplace(str, before, after) {
  let array = str.concat().split(" ");
  let index = array.indexOf(before);
  let support = array.splice(index, 1, after);
  function checkingFirstLetter (array) {
    if (str[index].charAt(0) == str[index].charAt(0).toUpperCase()) {
      let testing = array[index].charAt(0).toUpperCase();
      return array
      }
     }
  return array.join(" ");
}

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/67.0.3396.87 Safari/537.36.

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


#2

I see few problems here.

Is there any reason you are using concat before split here? split should be sufficient.

You are creating another function here but you are never using it.

Hope this helped you out!