+ 1
Can someone pls explain why this code is outputting 4, 6, 11, 5 ?
var arr = [4, 3, 2, 5]; for(var i = 1; i < 3; i++) { arr[i] = arr[i-1] + arr[i + 1]; } alert(arr);
7 Respostas
+ 4
Step 1:
i = 1
arr[1] = arr[0] + arr[2] // arr[1] = 4 + 2 = 6
Step 2:
i = 2
arr[2] = arr[1] + arr[3] // arr[2] = 6 + 5 = 11
alert(arr); // 4, 6, 11, 5
+ 3
Thank you yaroslav and Vasiliy :)
+ 2
1). arr[1] = arr[0] + arr[2] = 4 + 2 = 6;
2). arr[2] = arr[1] + arr[3] = 6 + 5 = 11;
arr = [4, 6, 11, 5];
alert(arr);
+ 2
Better say: "Большое Вам спасибо, Ярослав и Василий!" 😉
+ 1
Visualize your code execution
(Python, Java, C, C++, JavaScript, Ruby)
https://pythontutor.com
+ 1
Haha alright. Thank you very much, Yaroslav and Vasily! :)