Hello! Please help me figure out how to index the arr array in the following example (a training exercise that implements roulette):

<meta charset="utf-8"> <script> var arr = []; var rounds = 100; var zero = 0; var i = 0; for(i=0; i<=rounds; i++) { arr.push(Math.round(Math.random()*36)); } alert(arr); for (i=0; i<arr.length; i++) { if(arr[i]==0) { zero++; } } alert("Вероятность выпадания зеро: "+zero/arr.length*100+"%"); </script> 

Everything is in principle clear, except for a single moment: a condition in the body of the second cycle, which checks for the presence of zero if(arr[i]==0) . Why i, which was previously declared in the code as a normal variable, was used as a counter and was in no way associated with the arr array, suddenly became its index on which the presence of zero is checked. Thank!

  • 3
    because any variable can be used as a counter / index - Grundy

3 answers 3

Bracket notation is used to access array elements.

In square brackets there can be absolutely any string , no matter where it comes from: written directly or from any variable.

In this case, the variable i , because, due to the conditions of the cycle, it will change from the minimum index of the array to the maximum, which will allow you to check all the elements.

    If I understood the question correctly, then I need to understand how this cycle works.

     for (i=0; i<arr.length; i++) { if(arr[i]==0) { zero++; } } 

    In this loop, you see the presence of 0 in the arr array. To do this, you naturally need to go through the array, i.e. iterate over all elements and compare the element with 0 , and further along the code.

    In short, you enter the loop, assign i = 0 , look at arr[0] , if it is zero, consider, if not, increment i and repeat the iteration again.

    If not, then I think it is worth clarifying what is not clear and then it will be easier to understand how to deal with it)

    • Hello Darina! Thanks for your reply! I don’t understand why to check the conditions in the body of the loop (“Is the element of the array equal to zero?”) I use the variable i, which in the loop condition works simply as a counter. Why is this record (arr [i] == 0)? Indeed, in square brackets there can be only elements of the cycle, which in this case are a number sequence from 0 to 36, and not symbols of type i. Maybe I still just do not quite understand the syntax, so I'm sorry if the question seems silly to you. I would be very grateful if you help “chew” this moment - Michael
    • @Mikhail, After all , only elements of the loop can be in square brackets - no, there can be any string in square brackets. not symbols of type i - in this case i is not a string, it is a variable in which some value is stored - Grundy
    • @ Mikhail, it seems to me that you didn’t quite understand the arrays. The variable i is a variable in which some value lies. In this case, in arr [i], its value is substituted for i. If you wanted to substitute a character, you would do this: arr ['i'], but this is another topic. Understand more with arrays, for example, here learn.javascript.ru/array#selection of elements - Darina Goodwill
    • @DarinaGoodwill thanks! It seems I am beginning to understand: The variable i in this case works as a counter of indexes of the elements in the array. After each iteration, i becomes one greater (0,1,2,3 ... the maximum index value, which is taken from the value of arr.length). Then its value is substituted into arr [i] == 0, thus I iterate over the array indices. Right? - Michael
    • @ Mikhail, hooray! Did you understand! :) Then accept the answer :) - Darina Goodwill

    In this case, this is performance optimization (in this case, in my opinion, it is absolutely useless, although it does take place). i declared once and not 2:

     var arr = []; var rounds = 100; var zero = 0; for (var i = 0; i <= rounds; i++) { arr.push(Math.round(Math.random() * 36)); } alert(arr); for (var i = 0; i < arr.length; i++) { if (arr[i] == 0) { zero++; } } alert("Вероятность выпадания зеро: " + zero / arr.length * 100 + "%"); 
     <meta charset="utf-8"> 

    • in this code, nothing has changed compared to the code in question - Grundy
    • one
      The second time to write var for i does not make sense - Vladimir Gamalyan
    • one
      Since var is used here, exactly one variable will appear here - Grundy