0

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;
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Jem Stone
  • 13
  • 3
  • 1
    your pasted code needs some fixing. it's totally unclear what it looks like. Also, you tagged this as palindrome - you might want to mention why in your question – lucidbrot Nov 26 '17 at 19:58
  • Possible duplicate of [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Jem Stone Nov 26 '17 at 20:17

1 Answers1

1

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.

Troy Wray
  • 938
  • 5
  • 15