Triple Clicked explainer

The aim of this puzzle: Find the 3 copies of 'brush', and replace it with 'comb'.

Walkthrough of the solution: When you run the starter code, you can see the entire shoppingCart. If you look at the 3rd, 4th, and 5th items, they’re all 'brush'. You want to select that section and replace it with 'comb'. That’s a job for the .splice() method.

.splice() is attached to the array you want to modify, and it takes 3 arguments in its parentheses:

  1. The starting index of the selection
  2. How many items are in the selection
  3. The new item to be inserted in place of the selection

The starting index can be found by searching the array and using .indexOf(). Part of the code is completed for you: let index = shoppingCart. That will store the entire array in index, but it should really only store the index number of the first 'brush'. Attach .indexOf('brush') to the shoppingCart.

Now, you can use that index in a .splice() call. On a new line, between the let index = ... and the console.log(), add shoppingCart.splice(). The 1st argument should be the index. The next argument is the number of items to select, which is 3. Finally, you add the new item to insert, which should be the string 'comb'.

Your final command will be shoppingCart.splice(index, 3, 'comb').

Sample code solution:
(Tap below to reveal)

Since this is part of the final topic of the Arrays course and it is testing your knowledge, there is no sample solution code.

JavaScript Concepts: console.log(), import, .indexOf(), .splice()
Additional Code (hidden code that runs before the puzzle’s code):

let shoppingList = [

I don’t know what is wrong with this
Can someone please help me

Your code has the .splice() method attached to console.log(). What you want it to apply the .splice() method to the shoppingCart variable.

Try using shoppingCart.splice(index, 3, 'comb') on a line by itself, before using console.log(shoppingCart)


1 Like

image I don’t know what I’m doing wrong:

1 Like

I can help u. I am sending a screen short of mine


You made small mistake check the word` index’. You used string of index. Instead of that use the keyword index in your coding keyboard


Wow, thanks! That worked!!

1 Like

Please, explain why this solution doesn’t work? Thanks

It’s valid code, but you aren’t using the index variable in the .splice(), which is what the puzzle wants you to do.

Try replacing the 2 with index.

Hope this helps!

what is wrong with it?

I have completed this in my second try I’ll share a screenshot, but try to solve it your own :pray:

what is wrong

Don’t know what I did wrong anybody help

Así lo hice pero me marca que solo reemplaza uno y deja 2 brush y agrega combi
El código es por ejemplo
gasshopingcart.splice(‘index’,3,‘comb’) ya como resultado da combi, brush, brush, sponge

I solved this challenge as

So also why I have want visit here and want to post error in my challenge?

replace 2 to the index in the .splice()