Search and Replace problem with splice function

I wonder why array.splice(index, 1, after) doesn’t start at the index, remove the next string and add the after variable (which is the string we want to put instead of the word i remove with index, 1)

In the example I want to replace jumped by leaped.
I’ve checked, index is equal to 4 corresponding to fox so it should remove the next word, jumped

Instead what I have when i test is just [“fox”]

I’m a bit lost, on stackoverflow I saw people using splice has a mapping function but on mozilla I saw them use splice like a normal function (split for example)

function myReplace(str, before, after) {
  
  var splouf = str.split(" ");
  var index = 0;
  
  for (i = 0; splouf[i] !== before; i++) {
    index++; 
  }
  index--;
  
  var final = splouf.splice(index, 1, after);
  
  return final;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 OPR/50.0.2762.67.

Link to the challenge:
https://www.freecodecamp.org/challenges/search-and-replace

Ok I was looking at that example from the splice documentation when I start to think solving the problem that way.
What I understood is that splice will take the array myFish, go to index 2 so drum, remove 1 value so remove drum and then add (still at same index) the string trumpet.

image

So I was thinking that if I did

var final = splouf.splice(index, 1, after);

It will start at the index, remove 1 value and add the value of the variable after

I will try to do it another way but what you say is that splice just return return the value it takes out of an array, splice is like .pop but you can give specify the index you want to pop from ?

So splice can add a word too ?

Ok so I didn’t know a built in function could return two different things at the same time.
To me when I do the test the splice function put in my array the value it erase from it, how can I access the other return ?

After the above code runs, splouf looks like [ ‘The’, ‘rabbit’, ‘had’, ‘small’, ‘and’, ‘pointy’, ‘teeth’ ] and final has the value [ ‘large’ ].

Ok that’s what I tried to do

Ok I got it finally thanks !

Yep the index was not my main problem because I didn’t know if it was the good or not but I could have change that afterwards. I was stuck because I get as return the word I remove instead of the new string.

I was putting in the return of splice in a var (so the word remove) and then returning that var.
What I’m supposed to do is just return the splouf because splice as changed it when i did splouf.splice(index, 1, after)!

Really maybe it’s me who don’t have a “coder logic” but I don’t know how much time it would have took me to figure or even if I would have. When I read the documentation I always confuse myself …

So I now it work but as you said it doesn’t do the work for upper and lower case, now i’m going to think about a solution to that

One step at the time haha :slight_smile:

That’s what I’m saying to myself not to give up, each time I understand something I get closer to understand code logic. I’m still have a long journey in front of me and you help is valuable, you’ve already light many bulb for me :slight_smile:

Hope I won’t be a too slow learner

Thanks!