Jem Stone
Jem Stone

Reputation: 13

Explanation for what is happening in this certain for loop and if statement

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

Answers (1)

Troy Wray
Troy Wray

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

Related Questions