Guest User

Untitled

a guest
Jun 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. // Return true if the given string is a palindrome. Otherwise,
  2. // return false.
  3. // A palindrome is a word or sentence that's spelled the same
  4. // way both forward and backward, ignoring punctuation, case,
  5. // and spacing.
  6.  
  7. // Note
  8. // You'll need to remove all non-alphanumeric characters (punctuation,
  9. // spaces and symbols) and turn everything into the same case
  10. // (lower or upper case) in order to check for palindromes.
  11. // We'll pass strings with varying formats, such as "racecar",
  12. // "RaceCar", and "race CAR" among others.
  13. // We'll also pass strings with special symbols, such as "2A3*3a2",
  14. // "2A3 3a2", and "2_A3*3#A2".
  15.  
  16. function palindrome(str) {
  17. let isPalindrome=true;
  18. let myStr=str.toLowerCase().match(/[a-z0-9]/g);
  19. let myStrLen=myStr.length;
  20. for (let i=0; i<Math.round(myStrLen/2); i++) {
  21. if (myStr[i] != myStr[myStrLen-(i+1)]) {
  22. isPalindrome=false;
  23. break;
  24. }
  25. }
  26. // Good luck!
  27. return isPalindrome;
  28. }
  29.  
  30.  
  31.  
  32. palindrome("eye"); // true
  33. palindrome("race car"); // true.
  34. palindrome("not a palindrome"); // false.
  35. palindrome("A man, a plan, a canal. Panama"); // true.
  36. palindrome("never odd or even"); // true.
  37. palindrome("nope"); // false.
  38. palindrome("almostomla"); // false.
  39. palindrome("My age is 0, 0 si ega ym."); // true.
  40. palindrome("1 eye for of 1 eye."); // false.
  41. palindrome("0_0 (: /-\ :) 0-0"); // true.
  42. palindrome("five|\_/|four"); //false
Add Comment
Please, Sign In to add comment