Check Palindrom

Check Palindrom

### Given a string, check if it is a palindrom —

function checkPalindrom(inputString) {
  const originalWord = inputString.toLowerCase();
  const reversedWord = originalWord.split('').reverse().join('');

  return originalWord === reversedWord;
}

console.log(checkPalindrom('aabaa')) true
console.log(checkPalindrom('abac')) false
console.log(checkPalindrom('a')) true

⚙️ Implementation Details

📌 Parameters

inputString (String): The string to be checked to determine whether it is a palindrome (e.g., ‘aabaa’, ‘abac’, ‘a’).

🔍 Step-by-Step Logic

Convert to Lowercase: Use toLowerCase() to convert the input string into lowercase, ensuring the comparison is case-insensitive.

Split the String: Use split(‘’) to convert the string into an array of individual characters.

Reverse the String: Use reverse() to reverse the order of the characters in the array.

Join the Characters: Use join(‘’) to combine the reversed characters back into a string.

Compare the Strings: Use the strict equality operator (===) to compare the original lowercase string with the reversed string.

Return the Result: If both strings are identical, return true because the input is a palindrome. Otherwise, return false.

💡 Key Takeaway

A palindrome is a word or string that reads the same forwards and backwards. The function checks this by reversing the input string and comparing it with the original.

comments powered by Disqus