Help wanted with “Test your skills: Arrays 3”

Hi

Try as I might, I can’t figure this task out out.

The task is “Arrays 3” on this page - https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Test_your_skills:_Arrays

My code is:

https://jsfiddle.net/Davros23/yuq0hk8b/4/

From what I’ve read elsewhere, the line

name += (${index});

should update the names in the array from “name” to “name(0)” ,”name(1)” etc.

Why is it not working in this instance?

Any advice would be gratefully received.

Thanks,

Davva

Hi @Davva and welcome to the community :wave:

The loop variable name only exists during the current round of the loop. It’s a copy of the array element and not a reference. I like using for (const name of myArray) to always remind myself of that fact.

One way to solve this problem in the exercise is to use a forEach() loop in the form:

myArray.forEach(function(element, index) {
  /* your code */
});

Now you can use myArray[index] inside the loop to assign your new value to the actual array element.

I hope that helps. Feel free to ask more questions. :slightly_smiling_face:

Michael

Hi Michael,
Thanks for the explanation, and the welcome!
I’ll have a play around with myArray.forEach and "for (const x of myArray) to make sure I’ve got the distinction.
Thanks again!
Davva

1 Like