Hello, my code is working perfectly well in vscode, I’ve tried every text the projects asks you to test, for example: (A man, a plan, a canal. Panama), (0_0 (: /-\ 0-0) and the website detected they were a palindrome. but on the freecodecamp site when I copied my code it didn’t work, when I press check the text below the input doesn’t show up. Btw how can I make so when I click Enter it works like the button? Should I maybe instead of an arrow function make a normal function and add a onclick and a onkeydown attribute to my button and pass in the function? Thank you for the help!
Here’s my code:
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="styles.css" rel="stylesheet">
<title>Palindrome Checker</title>
</head>
<body>
<main>
<img class="logo" src="https://user-images.githubusercontent.com/20648924/133188813-5cceb6b3-a610-444c-b44c-4061bfc5b5c5.png">
<h1 class="title">Is it a Palindrome?</h1>
<div class="input-container">
<p class="description-text">Enter in text to check for a palindrome:</p>
<div class="input-btn-div">
<input type="text" id="text-input">
<button id="check-btn">Check</button>
</div>
<div class="result-div" id="result"></div>
</div>
<div class="palindrome-desc">💡A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.</div>
</main>
<script src="index.js"></script>
</body>
</html>
/* file: styles.css */
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
background-color: #0a0a23;
color: white;
font-size: 1.25rem;
}
main{
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.logo{
width: 400px;
height: auto;
}
.title{
font-size: 3rem;
margin-bottom: 1em;
}
.input-container{
background-color: white;
width: 400px;
height: 150px;
border-radius: 25px;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
box-shadow: 0 6px 6px #002ead;
margin-bottom: 1.5em;
}
.description-text{
color: #000;
text-align: center;
display: block;
margin: 10px;
}
input{
border: none;
border-bottom: 2px solid rgb(77, 0, 128);
margin-right: 10px;
width: 200px;
height: 30px;
line-height: 20px;
font-size: inherit;
}
button{
background-color: rgb(77, 0, 128);
color: white;
width: 90px;
height: 35px;
border-radius: 15px;
border: none;
cursor: pointer;
}
.palindrome-desc{
background-color: rgb(8, 83, 8);
width: 400px;
height: auto;
border-radius: 35px;
padding: 20px;
}
.result-div{
color: black;
font-size: 1.5rem;
margin: 10px;
}
/* file: script.js */
const inputElement = document.getElementById('text-input');
const buttonElement = document.getElementById('check-btn');
const result = document.getElementById('result');
buttonElement.addEventListener('click', ()=>{
const input = inputElement.value;
const correctedInput = input.toLowerCase().replace(/[^a-z0-9]/g, '');
const reversedInput = reverseString(correctedInput);
inputElement.value = '';
if(correctedInput === ''){
alert('Please input a value');
}
else if(correctedInput === reversedInput){
result.innerHTML = `<strong>${input}</strong> is a palindrome`;
}
else{
result.innerHTML = `<strong>${input}</strong> is not a palindrome`;
}
});
function reverseString(str) {
return str.split('').reverse().join('');
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Challenge Information:
Build a Palindrome Checker Project - Build a Palindrome Checker