Write code to check palindrome in a given string.
Anonymous
Here is a function that takes a string as input and returns true if the string is a palindrome, or false if it is not: function isPalindrome(string) { // Remove any non-alphanumeric characters and convert the string to lowercase const cleanedString = string.replace(/[^a-z0-9]/gi, '').toLowerCase(); // Reverse the string const reversedString = cleanedString.split('').reverse().join(''); // Compare the cleaned and reversed strings return cleanedString === reversedString; } Here's how you can use this function: const palindrome = 'racecar'; const nonPalindrome = 'hello'; console.log(isPalindrome(palindrome)); // Outputs: true console.log(isPalindrome(nonPalindrome)); // Outputs: false This code declares a palindrome and a non-palindrome string, and then calls the isPalindrome() function on each string. The function returns true for the palindrome string and false for the non-palindrome string.
Check out your Company Bowl for anonymous work chats.