Tell us what’s happening:
I need help with this code for the Palindrome Checker project. It won’t accept my code even though it is correct.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<title>Is it a Palindrome?</title>
<link rel= "stylesheet" href = "./styles.css">
</head>
<body>
<h1>Is it a Palindrome?</h1>
<div class= "searchbox">
<h2>Enter in text to check for a palindrome:</h2>
<input id="text-input">
<button type= "submit" id="check-btn">Check</button>
<div class="result">
<p id="result"></p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
body {background: #8165;}
h1 {text-align: center; color: #353590; margin: 70px}
.searchbox {border: 2px solid #124577; border-radius: 15px; width: 450px; height: 200px; text-align: center; margin-top: -10px;margin-left: 50px;background: #359090}
h2 {font-size: 1.1em;}
#text-input {margin: 10px; border: 2px solid #124577; border-radius: 10px;}
button{border: 2px solid #124577;
height: 20px; width: 40px; border-radius: 15px; background: rgb(255, 127, 127); font-size: 0.5em;}
.result {border: 2px solid #124577; border-radius: 15px; text-align: center; width: 150px; margin-left: 130px;}
/* file: script.js */
// Select necessary elements
const character = document.querySelector("#text-input");
const checkButton = document.getElementById("check-btn");
const result = document.getElementById("result");
// Regular expression to remove non-alphanumeric characters
const regex = /[^a-z0-9]/gi;
// Function to clean the string (remove non-alphanumeric characters and convert to lowercase)
function cleanCharacter(str) {
return str.replace(regex, "").toLowerCase();
}
// Function to check if input is empty and alert the user
function inputAlert(str) {
if (str === "") {
alert("Please input a value");
return true;
}
return false;
}
// Event listener for the check button
checkButton.addEventListener("click", () => {
const inputValue = character.value;
// If input is empty, show alert and return
if (inputAlert(inputValue)) return;
// Clean the input and check if it's a palindrome
const cleaned = cleanCharacter(inputValue);
const reversed = cleaned.split("").reverse().join("");
// Check if the cleaned string is a palindrome
if (cleaned === reversed) {
result.textContent = `"${inputValue} is a palindrome"`; // Wrap the entire result in quotes
} else {
result.textContent = `"${inputValue} is not a palindrome"`; // Wrap the entire result in quotes
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Challenge Information:
Build a Palindrome Checker Project - Build a Palindrome Checker