0
what is the output of this code?Explain in brief how. var arr = [1,2,3,4] var X = 1 for (var j=0, j <arr.length,j++){ x*=arr [j] } document write (x).
3 Réponses
+ 1
output:24
arr has 4 values with index 0,1,2,3
for Loop has the x=x*arr[j]
which indicates multiplication until arr.length is less then 4 it multiplies and gives final results as 24
0
0
0
if you change your declaration of the variable from "var X = 0" to "var x = 0", then it's 24
It would start as x=1 and go through all elements of the array and multiply it with the value of x, so basically x = 1 * 1 * 2 * 3 * 4 = 24
(as
x*= y
is the same as
x = x * y
)
But since you declare X = 1
and use the undeclared variable x
it won't show anything, as you're trying to multiply undefined with some value..