Assessement needed for "Test your Skills: Array 3"

Here’s my problem:
I find a method called splice(), so I think I just loop the whole array.
And here’s my code:

for (i = 1; i < myArray.length; i + 2) {
    for (j = 0; j < myArray.length; j++) {
        myArray.splice(i, 0, '(j)')

In my thought, I think this is just a loop in another one, but the page just stuck, and ask me to wait or quit.

I did it a lot in Python, I don’t understand why this can’t work in JS.

Now I know that there’s forEach() method in JS, thanks a lot to @ [chrisdavidmills].
But I still don’t understand why my way didn’t work out.

Thanks a looooot if it takes your time to look in or to solve my question.

By the way, my English is not that good, so if any expression I made makes you uncomfortable, I’m sorry, I didn’t mean that.

Hello @111111

splice as described here

can add element to array / remove element from array / replace element from array

replace mean replace the whole content not modify it

so you can achieve that by this code

for (i = 0; i < myArray.length; i++) {
myArray.splice(i, 1, myArray[i] + " (" +i+")");

thanks for letting me know about splice method :slight_smile:

hope that help and have a nice day :slight_smile:


Thank you for that! I was also stuck on this problem…

Can you please explain why the deleteCount parameter is logged as ‘1’ in you splice function? I thought I understood that the first parameter, start, tells splice where to start changing the array, and the second, deleteCount, indicates the number of elements in the array to remove from start? (Paraphrased from MDN Web Docs.)

you very welcome @SevenRooT

sure i will try

start is where splice will make it work (add/replace) as you said

deletecount is the number of how many element will be deleted so if start was 1 and deletecount was 5 then
it will delete element on index 1 2 3 4 5 of the array

if use 0 (by the way any negtive number will do the same ) it mean delete 0 element which mean do not delete so any item will be added to the array

i used delete to be 1 which mean delete one element and start was i that mean delete 1 element start from i which mean delete i

if i did not provide item then it will just delete but if i provide item it will insert this item start from this start (i)

since i used single item then it will insert just one element at start index which is i

extra case

if i provided more items then it will add more items at index start
so let us take example in case if we provide more item
array has 5 item
that will go to index 2 then delete 1 element which will be the element on index 2 then add element hi and hello starting from index then what one index 3 will be moved to index 4 and so on so the array will have 6 item not 5 now

so you can say in this case we insert element inside the array and move the other element based on that

so splice can

  1. add element if deletecount is 0 or negative or even removed
    2)replace element if deletecount is 1 or more
  2. modify which is not actually modify but it delete and add process in same time by read the element then do the modification then use the step 2 to do the replace

please notice item you can provide value or function which calculate the item as i did used the + operation to do the concatenation so if you have custom object you can use your custom method
and item still is parameter that why the splice has to call the concatenation or your custom method before call the splice one

so try to play it with each part and notice it behavior
not sure if i explained it in good way or not but let me know if i can do better

and have a nice day :slight_smile:

1 Like

Thanks a lot! And that really help!

you very welcome and glad to know that :slight_smile:

Thorough explanation!
Thank you for taking the time to write that out. I’ll be thinking over that for a while, but definitely got the basic concept of delete/add moving the items along down the array. :smiley:

thanks a lot and you very welcome and let me know if i can help with anything