Check if a String is a Palindrome


Instructions

Check if a given string is a palindrome. Return true if the string reads the same backward as forward, and false otherwise.


Examples

Here are a few examples to illustrate how the isPalindrome function works:

Example 1

Input: racecar Output: true

Example 2

Input: hello Output: false

Example 3

Input: A man a plan a canal Panama Output: true


Solution

The example solution provided demonstrates a function called isPalindrome that checks if a given string is a palindrome. The function converts the string to lowercase. It then compares the cleaned string to its reverse to determine if it reads the same backward as forward.

Check Palindrome

  const isPalindrome = (str) => {
    const cleanedStr = str.toLowerCase();
    const reversedStr = cleanedStr.split('').reverse().join('');
    return cleanedStr === reversedStr;
  }

Was this page helpful?