+ 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..
3 ответов
+ 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, ...
+ 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
+ 3
Ravindra Desai thanks for noticing, i forgot about this