Skip to content

Instantly share code, notes, and snippets.

@glinesbdev
Created May 8, 2015 17:34
Show Gist options
  • Save glinesbdev/4a3dd73d03309a3cb388 to your computer and use it in GitHub Desktop.
Save glinesbdev/4a3dd73d03309a3cb388 to your computer and use it in GitHub Desktop.
Palindrome Module
// [declare namespace]
var FourLife = FourLife || {};
// [declare module]
FourLife.PalindromeChallenge = (function() {
// [strict mode]
'use strict';
// [private variable(s)]
var _splitStr = [],
_reversedStr = '',
_status = false,
_regex = /[\W+_]/g;
// [private function(s)]
var _palindromeCheck = function(string) {
try {
for (var i = 0; i < string.length; i++) {
_splitStr = string.split('');
_reversedStr = _splitStr.reverse().join('');
}
_reversedStr = _reversedStr.replace(_regex, '').toLowerCase();
string = string.replace(_regex, '').toLowerCase();
_status = _reversedStr === string ? true : false;
_status = _status ? 'Yes' : 'No';
} catch (error) {
if (string === undefined || error instanceof TypeError) {
_status = 'You must provide a string.';
}
} finally {
return _status;
}
};
// [public method(s)]
return {
isPalindrome: _palindromeCheck
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment