Palindrome checker

Is there any way I can improve this code? :

// first filter out all non alphanumeric characters in the string
// then convert it to lowercase
// split the string
// reverse the string

// /[1]$/
// /[\w\s]/
function palindrome(str) {
const regex = /[\W_]
str = str.replace(regex, “”).toLowerCase()
const reversedStr = str.split(“”).reverse().join(“”)
return (str === reversedStr ? true : false)

console.log(palindrome(“0_0 (: /-\ :slight_smile: 0-0”));

  1. \w*\s* ↩︎

What are the two possible values for str === reversed? Do you think you could use those instead of having to manually specify them?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.