+ 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)

4 Answers

+ 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).

+ 9

The algorithm behind this question is to multiply all elements of the array. Output should be 24.

+ 6

Thanks everyone now i can go to challenge

+ 5

The output is 24