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

8th May 2020, 12:38 PM
Harold Adjahi
Harold Adjahi - avatar
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.
8th May 2020, 12:42 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
anh ouais c vrai
8th May 2020, 4:32 PM
Max Andy MOUMOUNI
Max Andy MOUMOUNI - avatar
0
Thank you very much Rick. It's clear to m'y mind now☺
8th May 2020, 12:55 PM
Harold Adjahi
Harold Adjahi - avatar
0
il manque des trucs à ton code
8th May 2020, 4:13 PM
Max Andy MOUMOUNI
Max Andy MOUMOUNI - avatar
0
par exemple là déclaration de i
8th May 2020, 4:13 PM
Max Andy MOUMOUNI
Max Andy MOUMOUNI - avatar
0
Oui Max Andy. J'ai omis d'écrire le i en voulant le déclarer dans la condition de la boucle for
8th May 2020, 4:17 PM
Harold Adjahi
Harold Adjahi - avatar
0
et aussi le alert(arr[i/2]) doit être dans la boucle for
8th May 2020, 4:19 PM
Max Andy MOUMOUNI
Max Andy MOUMOUNI - avatar
0
si non elle considère la dernière valeur du i qui est 3
8th May 2020, 4:19 PM
Max Andy MOUMOUNI
Max Andy MOUMOUNI - avatar
0
A la fin de la boucle le i est 4 car il est incrémenter par la suite.
8th May 2020, 4:21 PM
Harold Adjahi
Harold Adjahi - avatar
0
Thought array count start with zero 0123 which makes it 3*2=6
9th May 2020, 3:08 PM
Mercy Ayeni
Mercy Ayeni - avatar