# Explain how this code works?

https://jsfiddle.net/56be3vr8/
Please explain in detail what and why. Can't understand but want to)
The main thing I do not understand why two variables with a null value.
March 23rd 20 at 19:16
March 23rd 20 at 19:18
Solution
``````// Variable initialization massiv3 that references an array of 7 elements
let massiv3 = [-3, 100, 2, 0, -89, 34, 67];

// Initialize the variable sum with the value zero
let sum = 0;

// Loop that iterates the array of massiv3.
// On each iteration of the loop, the variable sum is added the value of the current iterated element of the array
for (
i = 0; // initialize loop counter (in the beginning of the cycle is equal to zero)
i < massiv3.length; // the running condition on each iteration. if it is true for the current counter value, the current iteration will execute, if false the iteration will fail and the loop will finish the job
i++ // expression which will be executed at the end of each iteration of the loop ("++" indicates the increase (increment) of counter i by one)
) {
sum += massiv3[i]; // add the value of the current iterated item of the array to ecosumo the value of the variable sum
}

// initialize the result variable with a value equal to the ratio of the value of the variable sum
// to the value of the length of the array
let result = sum / massiv3.length;

// output the value of result variable in the browser console
console.log(result);``````
*Circled by a red circle
I don't understand why here in the quadras. braces to insert a variable i if it is not in the array, and indeed why her to shove?
And as the cycle understands that it is necessary to add a track. array element to the sum? The sense switches on the trail.
Why doesn't he repeats the cycle with the first number? - kieran76 commented on March 23rd 20 at 19:21
@kieran76,
to insert a variable
no one anywhere or what not inserts.

`var array = [0, 1, 3, 'hello', 'foo']`
answer, how do I get the 4th element of this array? (the string "hello", the index 3) - gabriella.Feil commented on March 23rd 20 at 19:24
@gabriella.Feil, console.log(array)? - kieran76 commented on March 23rd 20 at 19:27
@kieran76, Yes.

now I want to get the string foo, but its index I will not tell you. but I will say that it lies in the variable i. in this case, I obtain the string foo?
``````var array = [0, 1, 3, 'hello', 'foo'];
var i = 4;``````
- gabriella.Feil commented on March 23rd 20 at 19:30
@gabriella.Feil, console.log(array[i])? - kieran76 commented on March 23rd 20 at 19:33
@kieran76, Yes. now comes?

if not, I will explain.

the cycle would repeat this line of code: `sum += massiv3[i];`

to repeat it it will be as long as this condition is true: `i < massiv3.length;`

moreover, in caedm a new repetition, the variable i will increase by one, so instead i here substituted to its value:
``````sum += massiv3;
sum += massiv3;
sum += massiv3;
sum += massiv3;
...``````

design `+=` is shorthand for from `sum = sum + massiv3[i];`

now it is clear? - gabriella.Feil commented on March 23rd 20 at 19:36
@gabriella.Feil, THANK you very much:D - kieran76 commented on March 23rd 20 at 19:39
@kieran76,
spoiler - gabriella.Feil commented on March 23rd 20 at 19:42

Find more questions by tags JavaScript