TYS: Array 3 (.forEach or .splice?)

Hi there,

I’m at the third exercise of Test Your Skills: Array.

After checking @chrisdavidmills github answer, I find out I was going the wrong way by not using .forEach method.

Instead, I was trying to use the .splice like this:

for (let i = 0; i < myArray.length; i++) { let arr = myArray.splice(1, 0, myArray.indexOf(i)); }

Unfortunately, my tab froze at the moment I enter the code, and I’m not able to test it (tried on Firefox and Chrome)!

Could somebody tell me if this method is working as well?

Thanks a lot! :slightly_smiling_face:

Hello,

If I am right, you push a new element into the array, with every cycle; infinite loop.
As I know, Array.splice(place,0,item) command is insert a new element.
You can try :
for (i in myArray) {
let arr = myArray[i];
}
or
let i = 0;
while (i<myArray.length) {
let arr = myArray[i];
i += 1;
}

Sometimes, when you work with large arrays, the for loop can heavly slow the process.

Gábor.

1 Like