0

javascript code

i don't understand the result of this code. can you help me, please? var k=[[0,6,4],[2,3,1],[7,2,2]]; var s=3; var i; for(i=0;i<k.length;i++){ s=s+k[i][2]; } alert(s)

9th Jan 2018, 3:25 PM
ilaria paron
1 Respuesta
+ 6
var k=[[0,6,4],[2,3,1],[7,2,2]]; var s=3; var i; for(i=0;i<k.length;i++){ s=s+k[i][2]; } alert(s) in this code output is 10 it came like this length of array k is 3 k=3 s=3 for loop is execute till i<k.length which is i<3so for i=0,1,2 the loop will execute so s=s+k[i][2] k[0]=[0,6,4] =>k[0][2]=4 k[1]=[2,3,1]=>k[1][2]=1 k[2]=[7,2,2]=>k[2][2]=2 then it execute like this s=s+k[0][2] which is equal to =>3+4=7 so s=7 s=s+k[1][2] which is equal to=>7+1=8 s=s+k[2][2] which is equal to=>8+2=10 so output become 10
9th Jan 2018, 4:00 PM
GAWEN STEASY
GAWEN STEASY - avatar