Advertisement
stoianpp

palindromes

May 15th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var text = "I remember the group ABBA, that brought lots of wow in the music world."
  2. text = text.replace(/\W/g, ' ');
  3. var array = text.split(' ');
  4. console.log(palindromsExtraction(array));
  5.  
  6. function palindromsExtraction(arr) {
  7.     var strToReturn = [];
  8.     for (var word in arr) {
  9.         arr[word] = arr[word].trim();
  10.         var isPalindrome = true;
  11.         for (var i = 0; i < arr[word].length / 2; i++) {
  12.             if (arr[word][i] !== arr[word][arr[word].length - 1 - i]) {
  13.                 isPalindrome = false;
  14.                 break;
  15.             }
  16.         }
  17.  
  18.         if (isPalindrome && arr[word].length>1)strToReturn.push(arr[word]);
  19.     }
  20.     return strToReturn;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement