Assesment needed for Test your skills 3 Arrays

I can’t find examples how to solve it
So i try it myself
Assesment wanted:

Link for task:


You’re solution works, but I would be careful using the .indexOf method as it only returns the first index at which a given element can be found. It works here but can be tricky in other situations.

If you need to use the index of an array when iterating I think it’s best practice using either:

myArray.forEach((value, index) => { do something })

or if you want to use a for of loop you can use:

for (let [index,value] of myArray.entries()) { do something }

or a good old for loop:

for (let index = 0; index < myArray.length; index++) { do something }

But for this case I think it’s best using the .map method as below:

So if you have :

let x = [“a”,“b”,“c”],index) => letter + index * 2) // = [ ‘a0’, ‘b2’, ‘c4’ ]

Or if you like one-liners:

[“a”,“b”,“c”].map((letter,index) => letter + index * 2).join(’#’) // = ‘a0#b2#c4’

Map Method

Also as a global advise, modifying an array while iterating over it can lead to strange behavior, especially if you start adding/removing items or if there is a more complex logic going on. You can do it, but it’s something to keep in mind.

1 Like