function reverse(str) { var reversed = ''; for (let el of str) { reversed = el + reversed; } return reversed; } console.log(reverse('baca'));
The out put of this code is acab
. but how?
As far as i know when i add a empty string with a value it will create an extra space after that but how it reversed? I have tried 1 day long for under standing reversed = el + reversed;
this line but i found that it will be b + ' ' + a + ' ' + c + ' ' + a
.
But how it reversed after return ...please help me.
It's a loop, think about how the loop is iterating over each character.
Iteration 1: 'b' + '' = 'b'
Iteration 2: 'a' + 'b' = 'ab'
Iteration 3: 'c' + 'ab' = 'cab'
Iteration 4: 'a' + 'cab' = 'acab'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.