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!