Last active
May 25, 2016 15:11
-
-
Save emanualjade/ac861198675a2a5db9c0a1916a33ab58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//===================== | |
// Palindrome | |
//===================== | |
function removeSpecialChars(str){ | |
return str.replace(/[^A-Z0-9]/ig, ""); | |
} | |
function lowerCase(str){ | |
return str.toLowerCase(); | |
} | |
// V1 | |
function checkPalindrome(str){ | |
return str == str.split('').reverse().join(''); | |
} | |
// V2 - recursive | |
function isPalindrome(str){ | |
if( str.length === 0 ){ return true; } | |
if( str[0] !== str[str.length-1] ){ return false; } | |
return isPalindrome( str.slice(1, str.length-1) ); | |
} | |
// USING A PIPELINE WITH REDUCE | |
var pipeline = [ | |
removeSpecialChars, | |
lowerCase, | |
checkPalindrome | |
]; | |
var valueToCheck = 'Race-car'; | |
var result = pipeline.reduce(function(acc, fn){ | |
return fn(acc) | |
}, valueToCheck); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment