Search and Replace problem with splice function

Search and Replace problem with splice function
0

#1

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:


#2

You should probably take a look at the splice documentation. The splice function returns an array of all of the items deleted. So the following line assigns [‘fox’] to final and you return final.

But why does your splice even replace the word ‘fox’ with the word ‘leaped’? That is because your logic is wrong in the following:

  for (i = 0; splouf[i] !== before; i++) {
    index++; 
  }
  index--;

Even fixing the above will not solve the challenge, because you have to consider the capitalization of the word your are replacing also. See the following challenge instructions note for more information about what is expected:

NOTE: Preserve the case of the original word when you are replacing it. For example if you mean to replace the word “Book” with the word “dog”, it should be replaced as “Dog”

Not sure where you checked the value, but index is 3 when the splice is performed. Put a console.log(index) on the line before the splice statement and you will see that it is 3.


#3

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


#4

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 ?


#5

You are correct in your logic here. The problem (as I pointed out in the previous reply), is that index did not have the correct value to remove the correct word.


#6

So splice can add a word too ?


#7

The splice function does two different things:

  1. Starting an index specified by the 1st parameter, It removes the number items specified in the 2nd parameter and adds any additional values following the 2nd parameter (i.e. 3rd, 4th, 5th, … nth). So the original array is changed

  2. It also returns an array with any items removed from splice.


#8

Consider the following code:

var splouf = ['The', 'rabbit', 'had', 'large', 'and', 'pointy', 'teeth'];
var index = 3;
var after = 'small';
var final = splouf.splice(index, 1, after); // removes the 4th element 'large' and adds 'small' to splouf as the 4th element

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


#9

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 ?


#10

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


#11

It only returns one value which is an array of any elements removed from the original array.

It also changes the original array on which the splice was used.


#12

You tried, but your code did not have the correct value for index.


#13

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 …


#14

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:


#15

Don’t worry. The more challenges you solve and documentation you read, the easier it will get. Everyone goes through the same struggles. Some spend less time struggling and some much more time. The great thing about FCC, is that you always have someone to help guide you to learn something new. Sometimes, it takes a couple of different ways of explaining the same thing for the light bulb to turn on.


#16

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!