Tell us what’s happening:
I don’t know why this doesn’t work. When I click the check button with or without text, nothing happens. Sometimes the console says “text is read-only”.
I know this isn’t the most efficient code because I have added and taken away things so many times trying to troubleshoot, I have lost track and don’t want to delete anything unnecessary.
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>
<h1>Is it a Palindrome?</h1>
<div class="box">
<p>Enter in text to see if it is a palindrome</p>
<input type="text" id="text-input"></input>
<button id="check-btn" type="button">Check</button>
<div id="result" class="hide"></div>
</div>
<div class="box">
<p>A palindrome is a word or phrase that can be read the same way forwards and backwards, ignoring punctuation, case, and spacing.</p>
</div>
<main>
<script src="./script.js"></script>
</body>
</html>
/* file: script.js */
const textInput = document.getElementById('text-input');
const text = textInput.value;
const checkButton = document.getElementById('check-btn');
const result = document.getElementById('result');
function cleanStringInput(str) {
const regex = /[_\W\s]/g;
return str.toLowerCase().replace(regex, '');
};
const checkPalindrome = () => {
if (text = null) {
result.innerHTML = `<span>Please input a value</span>`;
result.classList.remove('hide');
} else
if (text != null) {
const cleanTextValue = cleanStringInput(text);
const reverseTextValue = cleanTextValue.split('').reverse.join('');
if (cleanTextValue === reverseTextValue) {
result.innerHTML = `<span>${text} is a palindrome</span>`;
result.classList.remove('hide');
} else if (cleanTextValue !== reverseTextValue) {
result.innerHTML = `<span>${text} is not a palindrome</span>`;
result.classList.remove('hide');
}
}
};
checkButton.addEventListener("click", checkPalindrome);
/* file: styles.css */
* {text-align: center}
body {background: url(https://backdrop-shopify.imgix.net/https%3A%2F%2Fcdn.shopify.com%2Fs%2Ffiles%2F1%2F0051%2F6295%2F8946%2Ffiles%2Fpalindrome_teal_hero.jpg%3Fv%3D1686612945?ixlib=js-3.8.0&auto=format&q=75&w=3892&s=ff7e7ed42f873513f90d5807fae8de63)}
h1 {margin-top: 40px}
.box {
background: #89667c;
border-radius: 10px;
margin-left: auto;
margin-right: auto;
padding: 20px;
margin-bottom: 20px;
width: 80%
}
p {font-size: 20px}
input {height: 30px;
width: 35%}
button {height: 30px;
border-radius: 5px}
#result {
width: 50%;
height: 30px
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15
Challenge Information:
Build a Palindrome Checker Project - Build a Palindrome Checker