Assessment wanted for Arrays 1 skill test. (Tasks 1-4)

Assessment wanted for Arrays 1 skill test. (Tasks 1-4)

Link to task: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Test_your_skills:_Arrays#arrays_1

Links to my code:
Task-1: https://jsfiddle.net/EiNzp/0vxrqhc9/4/
Task-2: https://jsfiddle.net/EiNzp/rf7vq98y/1/
Task-3: https://jsfiddle.net/EiNzp/dhyzkcm3/2/
Task-4: https://jsfiddle.net/EiNzp/so9hpwue/1/

Thanks :wink:

Congratulations, @EiN!

All tasks work as expected. May I give you two alternatives for tasks 3 and 4 which may be cleaner?

Task 3
Since we need the index inside the loop it may be easier to use Array.prototype.forEach() in the form forEach((element, index) => { … }).

We could still use for...of, but we need Array.prototype.entries(). When using for (const [index, element] of myArray.entries()) { ... }, we have the index available inside the loop.

Task 4
Instead of the for...of loop we could use Array.prototype.filter().


`filter()` and `forEach()` (and some more similar functions) are a great way to go over an array without using an actual loop.

I hope that gives some inspirations. :wink:
Michael

1 Like

Oh thanks for your advice.
I went through the options you suggested and fixed my code. Here are the new links to tasks 3 and 4.

Task-3: https://jsfiddle.net/EiNzp/npjertg1/12/
Task-4: https://jsfiddle.net/EiNzp/2wbmshg7/4/

I have another question about task 3.
Do I understand correctly that these methods ( Array.prototype.forEach() and Array.prototype.entries() ) are only for viewing the elements of an array, and that using these methods you cannot change the elements of the array themselves, as I did in my original version?

P.S.
I realized that initially I did not quite understand the task correctly, and I thought that it was necessary to display the changed elements on the screen and with a separator. But apparently here it was necessary to look at them. And add a separator to the original array to check the understanding of this particular parameter. I need to learn to understand what exactly they want from me, although this time the misunderstanding did me good and added to my knowledge. :slightly_smiling_face:

1 Like

You are right that you can’t just change the element variable. It’s a copy of the respective element of the array. But that’s the same with the for..of loop you used in the original post. There you didn’t change elementOfMyArray directly (that’s not possible), but you used myArray[myArray.indexOf(elementOfMyArray)] to change the actual array element.

As how I understand the exercise you should actually change myArray and then create the string from it (like you did in your first version).

I hope my explanation is understandable. :slightly_smiling_face:

Michael

1 Like

Thanks for the explanation.
I made corrections to my code again. I hope that now I was able to correctly complete the task.
Here is a link to the corrected file: https://jsfiddle.net/EiNzp/npjertg1/19/

Thank you for your help and for your time, you are helping me a lot on my learning path.

1 Like

You’re welcome!
Both versions are looking good now. :+1:

1 Like