0
I didn't understand a result of a JavaScript challenge
Here is the code var arr = [4,5,3,2]; for (int=0; i<arr.length; i++) { arr[i] *= i; } alert(arr[i/2]); And the result is 6
10 odpowiedzi
+ 2
So basically, the for loop runs and it will multiple an element with whatever the value of variable i is at a time.
like
i = 0 and arr[i] = 4 so arr[i] *= i will make it 0,
this will happen for all the elements, at the end value of i will be 4 because that's the length of array.
now arr[i/2] is arr[2] which was 3 before loop run but after that it is now 6 because 3 * 2 is 6.
+ 1
anh ouais c vrai
0
Thank you very much Rick. It's clear to m'y mind now☺
0
il manque des trucs à ton code
0
par exemple là déclaration de i
0
Oui Max Andy. J'ai omis d'écrire le i en voulant le déclarer dans la condition de la boucle for
0
et aussi le alert(arr[i/2]) doit être dans la boucle for
0
si non elle considère la dernière valeur du i qui est 3
0
A la fin de la boucle le i est 4 car il est incrémenter par la suite.
0
Thought array count start with zero 0123 which makes it 3*2=6