Tell us what’s happening:
The code looks right, passes the conditions and gives the right output for different scenarios. But I’m unable to pass the test and move ahead.
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>Palindrome Checker</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<main class="container">
<h1 class="title">Is it a Palindrome?</h1>
<div class="palindrome-div">
<label for="text-input"
>Enter text to check for a palindrome:
</label>
<input id="text-input" value="" type="text" />
<button id="check-btn">Check</button>
<div class="results-div hidden" id="result"></div>
</div>
<div>
<p>
A <dfn>palindrome</dfn> is a word or sentence that's spelled the same
way both forward and backward, ignoring punctuation, case, and
spacing.
</p>
</div>
</main>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
body {
font-family: Arial, sans-serif;
text-align: center;
background-color:black;
margin: 0;
padding: 0;
color:white;
}
.container {
max-width: 600px;
margin: 50px auto;
background-color:black;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border: 3px solid white;
}
input {
width: calc(100% - 22px);
padding: 10px;
margin-bottom: 10px;
font-size: 16px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color:#bdbab3;
border-radius:15px;
}
#result {
font-size: 18px;
margin-top: 20px;
}
@media (max-width: 600px) {
.container {
padding: 10px;
}
}
/* file: script.js */
document.getElementById('check-btn').addEventListener('click', function() {
const inputString = document.getElementById('text-input').value.trim();
const resultElement = document.getElementById('result');
if (inputString === '') {
alert("Please input a value");
return;
}
const processedString = processString(inputString);
if (isPalindrome(processedString)) {
resultElement.textContent = `"${inputString}" is a palindrome`;
resultElement.style.color = "green";
} else {
resultElement.textContent = `"${inputString}" is not a palindrome`;
resultElement.style.color = "red";
}
});
function processString(str) {
// Remove non-alphanumeric characters and convert to lowercase
return str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
}
function isPalindrome(str) {
const len = str.length;
for (let i = 0; i < len / 2; i++) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Challenge Information:
Build a Palindrome Checker Project - Build a Palindrome Checker