Bonzar: Assessment wanted for Arrays skill test (task 1-4)

I looking forward to your feedback and comments
All results will be in the console.

Code link:
https://codepen.io/collection/ExvPyp

Task link:

And again everything is correct.

I have a remark for task 3:
Since we need the index inside the loop, it my be easier to use a forEach() loop instead of counting ourselves:

myArray.forEach(function(element, index) {
  myArray[index] = `${ element } (${index})`;
});

If using a for...of loop you should use const for the declaration of the loop variable: for (const name of myArray) {

Michael

1 Like

Thank you for your feedback, you pointed out an error in the loop, and I noticed that I also did not declare the variables nameIndex and myString (also task 3).

Why does it not cause errors and still work?

Good catch!

When you don’t use let or const the variable still gets created, but in the global scope. You generally don’t want this to happen. In contrast let and const create variables in the current block.
This means when you have two functions and in each you define a variable x = 0; (without let or const), that’s actually the same variable. If you do let x = 0 in each function, these variables are different and can’t influence each other.

Does that make it clearer?

1 Like

Yes, everything is clear now, thank you :sparkles:

1 Like