Reputation: 13
What is happening in this for
loop?
for (var i = 0; i < len/2; i++) {
Can some please explain exactly what is happening in this part — str[len - 1 - i]
— of the if
statement?
if (str[i] !== str[len - 1 - i]) {
return false;
Upvotes: 0
Views: 29
Reputation: 1018
It's comparing the first character with the last, then the 2nd with the last but one, up to the centre of the string and returning false if there's a difference, i.e. if it's not a palindrome.
Upvotes: 1