Tell us what’s happening:
I recently created a palindrome checker using HTML and JavaScript in Visual Studio Code, and it works perfectly there. However, when I copied and pasted the exact same code into the freeCodeCamp environment, it fails to function as expected.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Palindrome Checker</title>
</head>
<body>
<input type="text" id="text-input" placeholder="Enter text">
<button id="check-btn">Check</button>
<p id="result"></p>
</body>
</html>
/* file: styles.css */
/* file: script.js */
const input = document.getElementById('text-input');
const button = document.getElementById('check-btn');
const result = document.getElementById('result');
button.addEventListener("click", () => {
if (input.value === '') {
alert("Please input a value");
return;
}
const regex = /[^a-zA-Z0-9]/g;
const newText = input.value.replace(regex, '').toLowerCase();
const getPalindrome = (value) => {
const reversed = value.split('').reverse().join('');
return reversed;
};
if (newText === getPalindrome(newText)) {
result.innerText = `${input.value} is a palindrome.`;
} else {
result.innerText = `${input.value} 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/129.0.0.0 Safari/537.36
Challenge Information:
Build a Palindrome Checker Project - Build a Palindrome Checker