Solution Explanation please?: Check for Palindromes

Solution Explanation please?: Check for Palindromes
0

#1

Tell us what’s happening:
I saw this intermediate code solution on: freeCodeCamp Algorithm Challenge Guide: Check for Palindromes

function palindrome(str) {

  str = str.toLowerCase().replace(/[\W_]/g, '');
  for(var i = 0, len = str.length - 1; i < len/2; i++) {
    if(str[i] !== str[len-i]) {
      return false;
    }
  }
  return true;
}
palindrome("1 eye for of 1 eye.");
  • Question 1:
    Isn’t the condition expression in the For Loops, where i < len/2, skip the center-most letters during some test? for example, “almostomla”: str.length=10, ends up with i<4.5. therefore it never tests i=5 and i=6, and it would return true when the correct answer is false. Can anybody explain to me why does this solution still work? maybe it connects with my second question, but I just couldn’t make sense of it logically.

  • Question 2:
    When I tried to rewrite my own code after looking through the solution, I set return true within the if statement ( and yes I changed the if statement itself too ), and the test “1 eye for of 1 eye.” failed and gives me “True”. So if the only change I’ve made is to switch false/true placement, why should it matter? I realized there are something logical I’m missing, so can anybody please explain this to me?

Your code so far

function palindrome(str) {

  str = str.toLowerCase().replace(/[\W_]/g, '');
  for(var i = 0, len = str.length - 1; i < len/2; i++) {
    if(str[i] === str[len-i]) {
      return true;
    }
  }
  return false;
}
palindrome("1 eye for of 1 eye.");

Thanks for reading through my nonsense…:frowning: any help is appreciated guys!
Cheers


#2

Question #1 Response: If str.length is 10, then the following comparisons would be made for the “almostomla”

i=0 | str[0] = 'a | len-i=9 | str[9] = ‘a’ // true
i=1 | str[1] = ‘l’ | len-i=8 | str[8] = ‘l’ // true
i=2 | str[2] = ‘m’ | len-i=7 | str[7] = ‘m’ // true
i=3 | str[3] = ‘o’ | len-i=6 | str[6] = ‘o’ // true
i=4 | str[4] = ‘s’ | len-i=5 | str[5] =‘t’ // false so it is not a palindrome

As you can see above i=5 and i=6 are compared to i=4 and i=3 respectively.

Question #2 Response: You completely changed the logic. During the first iteration of the for loop, if the first and last letters are equal, you exit the function and return true. Once the return statement executes, it does not come back to finish iterating through the rest of the for loop.


#3

Indexes are zero-based. If you have a string of length 10, the first five characters (the first half) will have indexes 0, 1, 2, 3, and 4 (all less than 4.5). It doesn’t have to check for i = 5 because it belongs to the second half.

When looping through strings and arrays with a loop, you’d almost always see i < length - 1 used as the condition when i starts at 0.

If you checked if the two characters are equal in the for-loop, then it would immediately exit the function the moment it sees that two chars from both ends of the string are the same, all while not giving the correct output.

You can’t confirm if a string is a palindrome by just checking if one pair of chars are the same (which is what your modification does). But you can confirm a palindrome by checking if the chars in one pair are not the same. If it can’t find non-matching chars, then you can say that the string is indeed a palindrome.


#4

A post was split to a new topic: Need detailed explanation for how match function is used in Advanced solution of Check for Palindromes