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). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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).

14th Oct 2016, 2:07 AM
Vishwajeet Kumar Singh
Vishwajeet Kumar Singh - avatar
3 Answers
+ 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
14th Oct 2016, 3:01 AM
sreekar
0
0
14th Oct 2016, 12:56 PM
muku
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..
23rd Oct 2016, 4:00 PM
Andy Stutzmann