+ 2

Can someone please explain the output of this code for me?

c3 : function(){ let arr = [1,2,3]; let str = 'abc'; let x = 1; do { arr[x] = str[arr[x]]; x++; } while(x<arr.length-1); console.log(arr); } Now I know the output is an array with the items {1, "c", 3} But I don't know why..

8th Jan 2019, 2:32 PM
Phillip Mark England
Phillip Mark England - avatar
3 Antworten
+ 4
Do-while block take from array item with index x (second item, as x == 1) and sets it to third (as it takes 2 as index) letter of string. Then prints whole array. Hope it helps. Btw. Use better tags, for example: what language, issue, ...
8th Jan 2019, 3:30 PM
Maneren
Maneren - avatar
+ 3
The loop executes first without checking the condition (because its a do-while loop). arr[1]=str[2].. And then you increment x( now x=2)and the condition becomes false.Next you print the array
8th Jan 2019, 3:37 PM
Ravindra Desai
Ravindra Desai - avatar
+ 3
Ravindra Desai thanks for noticing, i forgot about this
8th Jan 2019, 5:45 PM
Maneren
Maneren - avatar