+ 1
Illustrate me why only str.length is not enough to reverse the string, why length-1 required ?
var str="string"; var emstr=""; for (i=str.length-1;i>=0;i--) emstr+=str[i]; console.log(emstr);
3 odpowiedzi
+ 4
index value starts from 0. 
so last index is 5 only . Should be str.length-1.
+ 2
Not 4 it's 5.
str.length returns 6 , the length of string. and index values are 0,1,2,3,4,5 ; Check this code :
for( let i=0;  i<str.length;  i++) 
console.log( "Index = "+i +" Value = "+str[i] ) ;
index 0 value  's' ;
... 
... 
so like :
          0 -> 's'
          1 -> 't'
          2 -> 'r'
          3 -> 'i'
          4 -> 'n'
          5 -> 'g'.
0
Then the 1st value of i is 4 and str[4] is n ....how it works?



