Intermediate Algorithm Scripting - Search and Replace

Tell us what’s happening:
Describe your issue in detail here.
so i know one of my issues is in trying to account for the case of the word that is being replaced.
I’ve created a copy of the original word called orgWord, i believe… that i intend to use for comparative purposes… but am struggling to create an actual “syntactical plan” here…
any help would be appreciated
Your code so far

function myReplace(str, before, after) {
  let arr = str.split(" ")

  for(let i = 0; i < arr.length; i += 1){
    let word = arr[i]
    let orgWord = word.slice()
    word = word.toLowerCase()
   
    if(word === before){
      let backHalf = arr.slice(i+1).join(" "); 
      let frontHalf = arr.slice(0, i).join(" ") + " " + after
        str = frontHalf + " " + backHalf
        console.log(str)

    }
      

  }

  
  return str;

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Search and Replace

Link to the challenge:

function myReplace(str, before, after) {
  let arr = str.split(" ")

  for(let i = 0; i < arr.length; i += 1){
    let word = arr[i]
    let orgWord = word.slice()
    word = word.toLowerCase()
   
    if(word === before && word === orgWord){
      let backHalf = arr.slice(i+1).join(" "); 
      let frontHalf = arr.slice(0, i).join(" ") + " " + after
        str = frontHalf + " " + backHalf

    }if(word === before && word != orgWord){
      after = after[0].toUpperCase() + after.slice(1)
      let backHalf = arr.slice(i+1).join(" "); 
      let frontHalf = arr.slice(0, i).join(" ") + " " + after
        str = frontHalf + " " + backHalf
    }
      

  }

  
  return str;

}
console.log(myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped"));
console.log(myReplace("Let us go to the store", "store", "mall"));
console.log(myReplace("Let us get back to more Coding", "Coding", "algorithms"))

most recent work

function myReplace(str, before, after) {
  let arr = str.split(" ")
  let index = str.indexOf(before)
  
  if(str[index] === str[index].toUpperCase()){
    after = after[0].toUpperCase() + after.slice(1)

  }else{
    after = after[0].toLowerCase() + after.slice(1)
    }

  
  for(let i = 0; i < arr.length; i += 1){
    let word = arr[i]
   
   
    if(word === before){
      let backHalf = arr.slice(i+1).join(" "); 
      let frontHalf = arr.slice(0, i).join(" ") + " " + after
        str = frontHalf + " " + backHalf

    }
  
    }
      

  

  
  return str;

}

//when i console.log(testCase) the output is on point?????

I get the sense you are doing a lot more work than you need to in order to avoid using regular expressions. And it is possible to use the replace method without involving regular expressions. Your code is going to be much simpler and cleaner if you use one (or both of these methods). Take advantage of what the language gives you for free.

The tests for this challenge are going very easy on you. They will only trigger one change in the string. After you get this working to pass the tests, if you really want to challenge yourself, try to make this function handle multiple changes in a sentence and avoid changes that aren’t words. For example:

myReplace('He said he would help', 'He', 'she')

should return She said she would help.

i sorta am :smiley: :slightly_smiling_face: :upside_down_face: :upside_down_face: :slightly_smiling_face: :smiley:
its because i can’t actually write in it with any level of personal trustworthyness.
i can very competently wield and regex expression as in put it where it goes… optimize what it does… ig at least for my level or w.ev… i think
but if i was asked to “write in it” i wouldnt have the first clue… and i have been tryyying to do this “authentically” ig… i dont know… overthinking something certainly Is an issue i deal with

I think you are familiar with the string replace method, aren’t you? You can solve this challenge with that method and no regular expressions. I would go that route.

was actually already on that… ima slap solved on this cuz i see where im going
thanks as always

But if you really want to make it easy on yourself then the regular expression test method would come in very handy.

Using the Test Method

You need the first letter of after to have the same case as the first letter of before. The test method makes it really easy to do this.

Once you have after set correctly then replace will do the rest of the work.

like \before\ to find the location of my slice point??

among other reasons

You didn’t give me enough information to understand what you are asking here.

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