Tell us what’s happening:
i normalized the input by removing non-alphanumeric characters and used replace(), reverse and join to compare palindrome, yet none is working. what am I not getting right
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Palindrome Checker</title>
</head>
<body>
<input id ="text-input"/>
<button id ="check-btn">Check</button>
<p id ="result"></p>
</body>
</html>
/* file: styles.css */
/* file: script.js */
const text = document.getElementById("text-input");
const checkBtn = document.getElementById("check-btn");
const result = document.getElementById("result");
checkBtn.addEventListener("click", () => {
const input = text.value.trim();
if(!input){
alert("Please input a value")
return;
}
//Normalize non-alphanumericals
const normalize = input.replace(/[^a-z0-9]/ig,"").toLowerCase();
//reverse non-alphanumericals
const reversed = normalize.split("").reverse().join("")
if(normalize === reversed){
result.textContent = `${input} is a palindrome`
}else{
result.textContent = `${input} is not a palindrome`
}
})
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Challenge Information:
Build a Palindrome Checker - Build a Palindrome Checker