var arr=[1,4,6,4,2]; for(var i=0; i<3; i++){ arr[i]=arr[i]+arr[i+2]; } document.write(arr[3]); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

var arr=[1,4,6,4,2]; for(var i=0; i<3; i++){ arr[i]=arr[i]+arr[i+2]; } document.write(arr[3]);

What is the output of this code? var arr=[1,4,6,4,2]; for(var i=0; i<3; i++){ arr[i]=arr[i]+arr[i+2]; } document.write(arr[0]); https://code.sololearn.com/WggijYpSg59h/?ref=app

10th Apr 2023, 11:30 AM
Manshu
Manshu - avatar
4 Answers
+ 4
4
10th Apr 2023, 11:30 AM
Manshu
Manshu - avatar
+ 6
What is your question? Run the code to see the output.
10th Apr 2023, 11:56 AM
Lisa
Lisa - avatar
+ 3
Lisa Can someone explain to me how to get 4 in the output....?🙄
10th Apr 2023, 1:58 PM
Manshu
Manshu - avatar
+ 2
/* Print each step out and compare with the result: */ let arr=[1,4,6,4,2]; for(let i=0; i<3; i++){ console.log(`${i}: ${arr[i]}+${arr[i+2]}`); // print each iteration arr[i]=arr[i]+arr[i+2]; } console.log(arr); // print result
10th Apr 2023, 2:05 PM
Lisa
Lisa - avatar