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);
2 odpowiedzi
+ 1
writes the string backwards
+ 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];
}