Guest User

Untitled

a guest
Apr 27th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. function palindrome(str) {
  2.  
  3. // Strip non-alphanumeric and convert to lowercase
  4. var string = str.replace(/[^0-9a-z]/gi, '').toLowerCase().split("");
  5.  
  6. // Iterate over half the number of chars in the string
  7. for(var i= 0; i < (string.length)/2; i++){
  8.  
  9. // Check i'th character from beginning and i'th character from end, accounting for 0.
  10. if(string[i] !== string[string.length-i-1]){
  11. return false; // False if any characters don't match
  12. }
  13. }
  14. return true; // True if palindrome
  15. }
Add Comment
Please, Sign In to add comment