+ 7

Help!! Can someone help me to solve this

I am stuck in this ques. for weeks...Always get these type of questions wrong..... The question is:- Find the output arr=[1,2,3,4] x=1 for (var i=0; i<arr.length;i++) x*=arr[i] document.write(x)

7th Apr 2017, 1:33 PM
Nikhil
Nikhil - avatar
4 Réponses
+ 5
"arr" has 4 elements 1,2,3,4 meaning arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr.length = 4. So, loop runs from i = 1 to i = 4 Iteration 1 : x = x * arr[0] = 1 * 1 = 1 Iteration 2 : x = x * arr[1] = 1 * 2 = 2 Iteration 3 : x = x * arr[2] = 2 * 3 = 6 Iteration 4 : x = x * arr[3] = 6 * 4 = 24 (Output).
7th Apr 2017, 1:49 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 9
The algorithm behind this question is to multiply all elements of the array. Output should be 24.
7th Apr 2017, 1:42 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
Thanks everyone now i can go to challenge
7th Apr 2017, 2:21 PM
Nikhil
Nikhil - avatar
+ 5
The output is 24
7th Apr 2017, 1:42 PM
Retul
Retul - avatar