Last active
January 24, 2024 05:11
-
-
Save thecodermehedi/b777c7caebc51b851eff87966af373e6 to your computer and use it in GitHub Desktop.
isPalindrome is a javascript function that takes a string as input and returns true if the string is a palindrome otherwise return false. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward, ignoring punctuation, case, and spacing.
This file contains hidden or 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
const isPalindrome = (string) => { | |
// replacing non-alphanumeric characters with space and converting to lowercase | |
cleanString = string.replace(/[^a-zA-Z0-9]/g, "").toLowerCase(); | |
// reversing the string (splitting it into an array, reversing the array, and joining it back into a string) | |
reversedString = cleanString.split("").reverse().join(""); | |
// comparing the reversed string with the original cleaned string and returning true or false | |
return cleanString === reversedString; | |
} | |
// Test cases | |
console.log(isPalindrome("level")); // Output: true | |
console.log(isPalindrome("hello")); // Output: false | |
console.log(isPalindrome("A man, a plan, a canal: Panama")); // Output: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment