0

Can someone help me interpret these code in words? .... Pls a clear elucidation..

var str = "123" var res = "" for(var i = str.length-1;i>= 0;i--){ res+=str[i]; } document.write(res);

12th Jan 2017, 11:20 AM
Ugwah chukwuemeka Emmanuel
Ugwah chukwuemeka Emmanuel - avatar
2 Respostas
+ 1
writes the string backwards
12th Jan 2017, 11:23 AM
Leon
Leon - avatar
+ 1
This code just copy the initial string but reversed. The result here is "321". The reasoning is the following : str.length-1 = 2; So we will assign each character of str to res until we get to the 0 index. So res = str[2]+str[1]+str[0]; You can write the for loop in this way if it help : for(var i=2; i>=0; i-1) { res = res + str[i]; }
12th Jan 2017, 11:28 AM
Guillaume BONHOMMEAU
Guillaume BONHOMMEAU - avatar