JavaScript, a flexible programming language, is frequently employed in web development to provide dynamic and interactive website elements.
A word, phrase, number, or string of characters is considered a palindrome if it reads the same both forward and backward, regardless of spaces, punctuation, or letter case. As an illustration, "level" and "mom" are palindromes.
To find out if a word has the same meaning when read backward, it is essential to check if it is a palindrome.
The ability to manipulate strings in JavaScript, along with comparison logic, makes palindrome checks quick and straightforward.
To check if a word is a palindrome in JavaScript by character comparison, we can follow these steps.
Convert the word to lowercase and remove non-alphabetic characters and whitespace. This step ensures the comparison is case-insensitive and eliminates any non-alphabetic characters that could interfere with the palindrome check.
Next, we iterate over the characters of the word from both ends simultaneously, comparing each pair of characters. From the first and last characters of the word, compare them to check if they are equal. Continue comparing subsequent pairs of characters moving toward the middle of the word.
If, at any point, a pair of characters is found to be different, it means that the word is not a palindrome, so we can immediately return false
.
If the loop completes without finding any unequal pairs, it means that all characters were equal, and the word is a palindrome, so we return true
.
Time complexity: O(N)
Space complexity: O(1)
function isPalindrome(word) {const cleanedWord = word.toLowerCase().replace(/[^a-z]/g, '');const length = cleanedWord.length;for (let i = 0; i < length / 2; i++) {if (cleanedWord[i] !== cleanedWord[length - 1 - i]) {return false;}}return true;}console.log(isPalindrome('level')); // Output: trueconsole.log(isPalindrome('hello')); // Output: false
To check if a word is a palindrome in JavaScript using reversed comparison approach, we can follow these steps:
First, use the toLowerCase()
method to change the word’s case to lowercase. By doing this, the comparison is guaranteed to be case-insensitive.
Then we use the replace()
method and a regular expression to eliminate non-alphabetic characters or whitespace from the word. By doing this, any potential discrepancies brought on by punctuation, spaces, or other non-alphabetic characters are removed.
Next, we reverse the word using the split(’’)
method to separate it into an array of distinct characters. Then the reverse()
method is used to flip the array’s order. Finally, the join(’’)
method rejoins the reversed array into a string.
The equality operator (===)
compares the reversed text to the original word. The term is a palindrome if they are identical; otherwise, they are not.
Time complexity: O(N)
Space complexity: O(N)
function isPalindrome(word) {const cleanedWord = word.toLowerCase().replace(/[^a-zÀ-ÖØ-öø-ÿ]/g, '');const reversedWord = cleanedWord.split('').reverse().join('');return cleanedWord === reversedWord;}// Example usage:console.log(isPalindrome('level')); // Output: trueconsole.log(isPalindrome('hello')); // Output: false
The regular expression pattern [^a-zÀ-ÖØ-öø-ÿ]
matches any character that is not a lowercase English alphabet (a-z) or any character from the extended Latin alphabet range (À-Ö, Ø-ö, ø-ÿ). This allows the cleaned word to retain special characters and non-English alphabets.
Both approaches provide valid solutions to check if a word is a palindrome. Approach 2 offers a more concise and optimized solution by leveraging JavaScript's built-in methods for string manipulation, while approach 1 performs character comparison manually.
Free Resources