Task: Write a function which returns true if the given string is a palindrome. Otherwise, return false.
Pseudocode:
- Get the string value as a parameter
- If the user has entered a string
- Remove non-alphanumeric characters and make the string lowercase
- Reverse the string
- If the string is a palindrome
- return the boolean value true
- If the value is not a palindrome
- return the boolean value false
- If the given value is not a string
- Ask the user to enter a string
- If the user has entered a string
Actual Code:
function palindrome(str) {
if (typeof str === 'string') {
// remove non-alphanumeric characters and make str lowercase
var removeChar = str.replace(/[^A-Z0-9]/ig, "").toLowerCase();
// reverse str variable
var checkPalindrome = removeChar.split('').reverse().join('');
// check if str is a palindrome
if (removeChar === checkPalindrome ) {
return true;
} else {
return false;
}
} else {
return "Please enter a string";
}
}
palindrome("car"); // false