Search and Replace

Hello everyone,

I try to solve “Search and Replace” algorithm question through using filter and map method. However, its unsuccessful attempt, and I cannot find where is my mistake. Everything seems perfect. Could u help me?

function myReplace(str, before, after) {
  let splitted = str.split(" ");
  return splitted
  .filter(word => word == before)
  .map(function(word) {
    if(word[0] == word[0].toUpperCase()){
      after = after[0].toUpperCase() + after.slice(1)
      word = after
      return word
    } else {
      word = after.toLowerCase()
      return word
  .join(" ")

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

you are returning splitted immediately and not running the rest of the function.
after that i suggest you break the into different lines/segments and console.log things along the way to see what is happening in each step. (eg seperate .filter from .map and log results of each independently, one step at a time).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.