Upon reviewing your code, it appears that you are always returning true
regardless of the input. It is essential to validate whether the given string matches its reversed version.
function checkPalindrome(str) {
var cleanString = (str+'').replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~\ ()]/g,"").replace(/\s/g, "").toLowerCase();
return cleanString === (cleanString.split('').reverse().join(''));
}
Explanation
- Initially, we create a private variable named
cleanString
. By converting the input into a string (+''
) and applying replacement operations, we ensure unwanted characters are removed.
- The subsequent conditional checks if
cleanString
matches its reversed form. The expression .split('').reverse().join('')
reverses the string for comparison, with the result determining the return value.
Test Cases:
checkPalindrome(151);
➥ true
checkPalindrome('eye');
➥ true
checkPalindrome(2552);
➥ true
checkPalindrome(12);
➥ false
checkPalindrome('foo');
➥ false