Search and Replace challenge, my code is not working

function myReplace(str, before, after) {
var args = [...str];
  var first = "";
  str = str.split(" ");
  for (var i = 0; i < str.length; i++) {
    if (str[i] === args[0]) {
 return first += str[i];
    }
  }
  return str.replace(first, args[1]);
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

There might be something wrong with your code.

What have you tried to diagnose the problem?

I don’t know how to diagnose the problem

console.log is the first port of call.
Also try putting the code into something like codepen and running it there or paste into the console in developer tools (chrome).

The problems start on line two and the rest isn’t looking great but it can be fixed.

SyntaxError: illegal character
That’s the message I got

You’re on the right path. Correct the syntax errors or you won’t be able to execute the program. When it doesn’t fail any more, use console.log to see what variables come into the function and what content comes out of it.

I can tell you most of the code you’ve put in is not needed or not correct, if you check in the console after you’ve added the code in a few places, you’ll pick up on the errors and be able to correct them.

I can’t seem to find the syntax errors. been battling with it for long now

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

does it say which is the line with the issue? it should indicate where the error is
if you need you can post a screenshot and we can help
you interpretate the error message

I see it on the second linesyntax

I think it is from the function call

you have “” in the function call and that is an illegal character, you need the straight quotes ""

it may be something else… correct that for now, see if the syntax error appear again.
would you be willing to share your pen?

oh! I have corrected that, No more syntax error but the code doesn’t work yet
pen --> https://codepen.io/samivee/pen/JjYRavV

now that there are no more syntax errors, I suggest you try to debug your code using console.log statements
or, if you are not familiar with that method, you could also use this tool:
http://pythontutor.com/javascript.html#mode=edit

try to look at what happens. How each value change at each step. What is returned from the function.

Try to compare that to what you would expect.

1 Like

The tool was very helpful, thanks. I have been able to adjust my code to a large extent but I still can’t preserve the case of the first letter of the original word


 function myReplace(str, before, after) {
  var args = [before, after];
 var first = "";
var final = "";
str = str.split(" ");

for (var i = 0; i < str.length; i++) {
 if (str[i] === args[0]) {
           first = str.indexOf(str[i]);  
 final = str.slice(0, first).join(" ") + " " + args[1].charAt(0).toUpperCase() + args[1].slice(1) + " " + str.slice(first + 1).join(" ");
        }
      }
 return final;
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

before preserving it you will need to check what the original casing was. You are just uppecasing the letter with no check

also, do you really need to define args when you can just keep referencing before and after?

I don’t know how to check the casing on the original word and use it on the replacement word.

try googling it, see if you can find it on your own

I passed 2 and failed 3. still not totally working



function myReplace(str, before, after) {

  

  var args = [before, after];

      var first = "";

      var final = "";

      var textChoice = "";

      str = str.split(" ");

      for (var i = 0; i < str.length; i++) {

        if (str[i] === args[0]) {

           first = str.indexOf(str[i]); 

        if (args[0][0] === args[0][0].toUpperCase()) {

        textChoice += args[1].charAt(0).toUpperCase();

        } 

        else if (args[0][0] !== args[0][0].toUpperCase()) {

        textChoice += args[1].charAt(0).toLowerCase();

        }

      

      final = str.slice(0, first).join(" ") + " " + textChoice + args[1].slice(1) + " " + str.slice(first + 1).join(" ");

          

        }

         

      }

      return final;

}

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

this is totally unnecessary
why are you creating the array?
you can reference before and after in your code instead


this is a bad name choice, you create an array but still call it str?
you could do let arr = str.split(" ")


what kind of debugging have you tried?
have you tried console.log to see what your function returns?
you can even add in the function before the return statement
console.log({input: {str, before, after}, output: {final}}) to see input and output of your function , and then run the tests to see it for each function

have you tried pythontutor to see what happens at each step?

How do I reference before and after directly in the code. it’s not working for me :exploding_head:



function myReplace(str, before, after) {

        

      var first = "";

      var final = "";

      var textChoice = "";

      let arr = str.split(" ");

      for (var i = 0; i < arr.length; i++) {

        if (arr[i] == before) {

           first = arr.indexOf(str[i]);

      

      if (before[0] == before[0].toUpperCase()) {

        textChoice += after.charAt(0).toUpperCase();

        } 

      else if (before[0] != before[0].toUpperCase()) {

        textChoice += after.charAt(0).toLowerCase();

        }

          final = arr.slice(0, first).join(" ") + " " + textChoice + after.slice(1) + " " + arr.slice(first + 1).join(" ");

}        

        

      }   

      

      return final;

}

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